pro e to excel linkage - new
  Home
  Contact
  new
<HTML>
<SCRIPT LANGUAGE="JavaScript" type=text/javascript>
/*
   HISTORY

14-NOV-02   J-03-38   $$1   JCN      Submitted.
07-MAR-03   K-01-03   $$2   JCN      UNIX support
 */

function isProEEmbeddedBrowser ()
{
  if (top.external && top.external.ptc)
    return true;
  else
    return false;
}

function pfcIsWindows ()
{
  if (navigator.appName.indexOf ("Microsoft") != -1)
    return true;
  else
    return false;
}

function pfcCreate (className)
{
  if (!pfcIsWindows())    
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
 
  if (pfcIsWindows())
    return new ActiveXObject ("pfc."+className);
  else
    {
      ret = Components.classes ["@ptc.com/pfc/" + className + ";1"].
    
    createInstance();
      
      return ret;
    }
}

function pfcGetProESession ()
{
  if (!isProEEmbeddedBrowser ())
    {
      throw new Error ("Not in embedded browser.  Aborting...");
    }
 
  // Security code
  if (!pfcIsWindows())
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
 
  var glob = pfcCreate ("MpfcCOMGlobal");
  return glob.GetProESession();
}

function pfcGetScript ()
{  
  if (!isProEEmbeddedBrowser ())
    {
      throw new Error ("Not in embedded browser.  Aborting...");
    }
 
  // Security code
  if (!pfcIsWindows())
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
 
  var glob = pfcCreate ("MpfcCOMGlobal");
  return glob.GetScript();
}

function pfcGetExceptionType (err)
{
  if (pfcIsWindows())
    return (err.description);
  else
    {
      errString = err.message;
   // This should remove the XPCOM prefix ("XPCR_C")
      if (errString.search ("XPCR_C") == 0)
          return (errString.substr(6));
      else
    return (errString);
    }
}
      

</SCRIPT>
<SCRIPT LANGUAGE="JavaScript" type=text/javascript>
function GetData () {

    if (!pfcIsWindows())
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

    var data_elem = document.getElementById("data");
    var session = null;
    var model = null;

    // Get session object
    try { session = pfcGetProESession(); }
    catch (e) {
        data_elem.innerHTML = "ERROR: Cannot connect to Pro/Engineer session!";
        return;
    }

    // Make sure there is a model active
    try { model = session.CurrentModel; }
    catch (e) {
        // probably no model
        data_elem.innerHTML = "Problem getting current model info.";
        return;
    }

    data_elem.innerHTML = "<br>" + "Top Level Model: " + model.FileName;

    // Setup appdata object for bom data
    var appdata = new Object();
    appdata.params = new Array( "LEVEL", "NAME", "QTY", "DESCRIPTION", "MODELED_BY" );
    appdata.comppath_seq = new pfcCreate("intseq");
    appdata.root = model;

    // get bom data as an array of arrays
    appdata.values = GetBOMData(model, appdata);

    // send bom data
    SendData(appdata);
}



function GetBOMData ( model, appdata ) {

    var data_elem = document.getElementById("data");
    var status_elem = document.getElementById("status");

    var model_array = new Array(); // data for this model
    var return_array = new Array(); // array to store model_array's

    // Assign parent attribute for qty count
    //
    try {
        model_array["PARENT"] = appdata.parent.FileName;
    }
    catch (e) {
        // ignore exception, probably top-level asm
        model_array["PARENT"] = "";
    }


    // Get params of current model
    //
    for (var i = 0; i < appdata.params.length; i++) {
 
        if (appdata.params[i] == "LEVEL") {
            model_array["LEVEL"] = appdata.comppath_seq.Count+1;
        }
        else if (appdata.params[i] == "NAME") {
            model_array["NAME"] = model.FileName;
        }
        else if (appdata.params[i] == "QTY" && model == appdata.root) {
            model_array["QTY"] = 1;
        }
        else {
            var param = null;
            var paramvalue = " -- n/a -- ";

            try {
                // get parameter object
                param = model.GetParam(appdata.params[i]);

                // get parameter value
                switch (param.Value.discr) {
                  case pfcCreate("pfcParamValueType").PARAM_STRING:
                    paramvalue = param.Value.StringValue;
                    break;
                  case pfcCreate("pfcParamValueType").PARAM_INTEGER:
                    paramvalue = param.Value.IntValue;
                    break;
                  case pfcCreate("pfcParamValueType").PARAM_BOOLEAN:
                    if (param.Value.BoolValue)
                      paramvalue = true;
                    else
                      paramvalue = false;
                    break;
                  case pfcCreate("pfcParamValueType").PARAM_DOUBLE:
                    paramvalue = param.Value.DoubleValue;
                    break;
                }
            }
            catch (e) {
                // param probably doesn't exist, ignore
            }

            // store param value in model array
            model_array[appdata.params[i]] = paramvalue;
        }
    }

    // store model array in return array
    return_array.push(model_array);

 
    // Recurse into components, if model is an assembly
    //
    if ( model.Type == pfcCreate("pfcModelType").MDL_ASSEMBLY ) {
 
        var compMdl = null;
        var qtyIndexName = null;
        var qtyCount = new Array();

        // get component sequence of current subasm
        var components = model.ListFeaturesByType( false, pfcCreate("pfcFeatureType").FEATTYPE_COMPONENT );
 
        // loop through components
        for (var i = 0; i < components.Count; i++) {
 
            var compFeat = components.Item(i);
 
            if (compFeat.Status != pfcCreate("pfcFeatureStatus").FEAT_ACTIVE) {
                continue; // skip inactive components
            }

            // Append component id to sequence (for building ComponentPath)
            appdata.comppath_seq.Append(compFeat.Id);
 
            // get model object of component
            try {
                // have to create ComponentPath object first, then use "Leaf" property
                var cp = pfcCreate("MpfcAssembly").CreateComponentPath( appdata.root, appdata.comppath_seq );
                compMdl = cp.Leaf;
            } catch (e) {
                status_elem.innerHTML += "<br> CreateComponentPath() exception: " + pfcGetExceptionType(e);
            }

            // using a unique index (subasm & comp names) for the qty count array
            qtyIndexName = model.FileName+"/"+compMdl.FileName
            appdata.parent = model;

            // Descend into subassembly, if model has not been processed in this subasm
            if ( !(qtyIndexName in qtyCount) && compMdl != model ) {
                // concatenated arr into return_array (concat doesn't seem to work)
                var arr = GetBOMData(compMdl, appdata);
                for (var j=0; j<arr.length; j++) {
                    return_array.push(arr[j]);
                }
                arr = null;
            }

            // initialize and increment qty count for this subasm/component
            if ( ! (qtyIndexName in qtyCount) ) {
                qtyCount[qtyIndexName] = 0;
            }
            qtyCount[qtyIndexName]++;
 

            // Remove last id in sequence, not needed anymore
            try {
                appdata.comppath_seq.Remove( (appdata.comppath_seq.Count-1), (appdata.comppath_seq.Count) );
            } catch (e) {
                status_elem.innerHTML += "<br> comppath_seq.Remove exception: " + pfcGetExceptionType(e);
            }
 
        } // Loop: components


        // process arrays (for qty adjust) returned from GetBOMData() call
        for (var i = 1; i < return_array.length; i++) {
 
            var compName = return_array[i]["NAME"];
            qtyIndexName = model.FileName+"/"+compName;

            // Adjust qty for current level objects
            if (return_array[i]["PARENT"] == model.FileName) {
                for (var j = 0; j < appdata.params.length; j++) {

                    // make sure qty was requested
                    if (appdata.params[j] == "QTY") {
                        if (qtyIndexName in qtyCount) {
                            return_array[i]["QTY"] = qtyCount[qtyIndexName];
                        }
                        else {
                            return_array[i]["QTY"] = 1;
                        }
                    }

                }
            }
        }

        qtyCount = null;

    } // model.Type

    return return_array;
}



function SendData ( appdata ) {

    var oXL = null;
    var data_elem = document.getElementById("data");

    if (appdata.values.length == 0) {
        data_elem.innerHTML = "No data to send!";
        return;
    }

    if (pfcIsWindows()) {

        // Get/Create Excel Object Reference
        try {
            oXL = GetObject("","Excel.Application"); // Use current Excel session
        }
        catch (e) {
            // couldn't get an excel session, try starting a new one
            try {
                oXL = new ActiveXObject("Excel.Application"); // Open new Excel session
            }
            catch (e) {
                // couldn't start a new excel session either
            }
        }

        if (oXL == null) {
            data_elem.innerHTML = "Could not get or start Excel session!";
            return;
        }

        // Create new workbook
        try {
            oXL.Visible = true;
            var oWB = oXL.Workbooks.Add();
            var oSheet = oWB.ActiveSheet;
        }
        catch (e) {
            data_elem.innerHTML = "Problem creating new workbook.";
            return;
        }

        // Write header cells
        for (var i=0; i < appdata.params.length; i++ ) {
            oSheet.Cells(1, i+1).Value = appdata.params[i];
        }

        // Write data cells
        for (var i=0; i < appdata.values.length; i++ ) {
            for (var j=0; j < appdata.params.length; j++ ) {
                oSheet.Cells(i+2, j+1).Value = appdata.values[i][appdata.params[j]];
            }
        }
    }
    else {

        // Not a windows platform, write data to browser

        // Write header cells
        data_elem.innerHTML += appdata.params.join(" &nbsp; / &nbsp; ");

        // Write data cells
        for (var i=0; i < appdata.values.length; i++ ) {
            data_elem.innerHTML += "<br>";
            for (var j=0; j < appdata.params.length; j++ ) {
                if (j > 0) { data_elem.innerHTML += " / "; }
                data_elem.innerHTML += appdata.values[i][appdata.params[j]];
            }
        }

    }

}



 function Clear() {
    var data_elem = document.getElementById("data");
    var status_elem = document.getElementById("status");
    data_elem.innerHTML = "";
    status_elem.innerHTML = "";
}</SCRIPT>
<BODY>

<form name="f">

<br><INPUT id="get_btn" type=button value="Get BOM" onclick="GetData()">
<INPUT id="clr_btn" type=button value="Clear" onclick="Clear()">
<br><div id="data"></div><br>
<br><div id="status"></div><br>

</form>

</BODY>
</HTML>
Today, there have been 1 visitors (3 hits) on this page!
This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free