0 Replies Latest reply on Dec 19, 2014 2:40 AM by Tai Nguyen

    Using cost API but got an error

    Tai Nguyen

      I'm writing a small C# app to test the cost API but got an error at line:

       

      swCosting = (CostManager)swModelDocExt.GetCostingManager();

       

      Precondition:

      1/ Start Solidworks software and open one sample file

      2/ Add reference in c:\Program Files\SolidWorks Corp\SOLIDWORKS\api\redist\ to my C# app.

      SolidWorks.Interop.sldcostingapi.dll

      SolidWorks.Interop.swconst.dll

      SolidWorks.Interop.sldworks.dll

       

      Please help to take a look at below code (the code is copy from the help of Solidworks)  except line #18, #19.

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using SolidWorks.Interop.sldworks;
      using SolidWorks.Interop.swconst;
      using SolidWorks.Interop.sldcostingapi;
      using System.Diagnostics;
      using System.Runtime.InteropServices;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  SldWorks swApp;
                  swApp = (SldWorks)Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application.23"));
                  //swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application");
      
                  ModelDoc2 swModel = default(ModelDoc2);
                  ModelDocExtension swModelDocExt = default(ModelDocExtension);
                  CostManager swCosting = default(CostManager);
                  CostPart swCostingPart = default(CostPart);
                  CostBody swCostingBody = default(CostBody);
                  object swCostingModel = null;
                  CostAnalysis swCostingAnalysis = default(CostAnalysis);
                  CostAnalysisMachining swCostingMachining = default(CostAnalysisMachining);
                  string machiningCostingTemplatePathName = null;
                  string machiningCostingReportTemplateFolderName = null;
                  int nbrMachiningCostingTemplate = 0;
                  int nbrCommonCostingTemplate = 0;
                  object[] commonCostingTemplates = null;
                  object[] machiningCostingTemplates = null;
                  int nbrCostingBodies = 0;
                  object[] costingBodies = null;
                  string costingBodyName = null;
                  int i = 0;
                  bool isBody = false;
                  CostFeature swCostingFeat = default(CostFeature);
                  CostFeature swCostingNextFeat = default(CostFeature);
                  CostFeature swCostingSubFeat = default(CostFeature);
                  CostFeature swCostingNextSubFeat = default(CostFeature);
      
                  swModel = (ModelDoc2)swApp.ActiveDoc;
                  swModelDocExt = (ModelDocExtension)swModel.Extension;
      
                  // Get Costing templates names
                  Debug.Print("Costing template folders:");
                  machiningCostingTemplatePathName = swApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swFileLocationsCostingTemplates);
                  Debug.Print("    Name of Costing template folder: " + machiningCostingTemplatePathName);
                  machiningCostingReportTemplateFolderName = swApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swFileLocationsCostingReportTemplateFolder);
                  Debug.Print("    Name of Costing report template folder: " + machiningCostingReportTemplateFolderName);
                  Debug.Print("");
      
                  // Get CostingManager
                  swCosting = (CostManager)swModelDocExt.GetCostingManager();
                  swCosting.WaitForUIUpdate();
      
                  // Get the number of templates
                  nbrMachiningCostingTemplate = swCosting.GetTemplateCount((int)swcCostingType_e.swcCostingType_Machining);
                  nbrCommonCostingTemplate = swCosting.GetTemplateCount((int)swcCostingType_e.swcCostingType_Common);
      
                  // Get names of templates
                  machiningCostingTemplates = (object[])swCosting.GetTemplatePathnames((int)swcCostingType_e.swcCostingType_Machining);
                  commonCostingTemplates = (object[])swCosting.GetTemplatePathnames((int)swcCostingType_e.swcCostingType_Common);
      
                  Array.Resize(ref machiningCostingTemplates, nbrMachiningCostingTemplate + 1);
                  Array.Resize(ref commonCostingTemplates, nbrCommonCostingTemplate + 1);
      
                  Debug.Print("Costing templates:");
      
                  // Print names of templates to Immediate window
                  for (i = 0; i <= (nbrMachiningCostingTemplate - 1); i++)
                  {
                      Debug.Print("    Name of machining Costing template: " + machiningCostingTemplates[i]);
                  }
      
                  Debug.Print("");
      
                  for (i = 0; i <= (nbrCommonCostingTemplate - 1); i++)
                  {
                      Debug.Print("    Name of common Costing template: " + commonCostingTemplates[i]);
                  }
      
                  Debug.Print("");
      
                  // Get Costing part
                  swCostingModel = (object)swCosting.CostingModel;
                  swCostingPart = (CostPart)swCostingModel;
      
                  // Create common Costing analysis
                  swCostingAnalysis = (CostAnalysis)swCostingPart.CreateCostAnalysis("c:\\program files\\solidworks corp\\solidworks\\lang\\english\\costing templates\\multibodytemplate_default(englishstandard).sldctc");
      
                  // Get common Costing analysis data
                  Debug.Print("Common Costing analysis data:");
                  Debug.Print("    Template name:  " + swCostingAnalysis.CostingTemplateName);
                  Debug.Print("    Currency code: " + swCostingAnalysis.CurrencyCode);
                  Debug.Print("    Currency name: " + swCostingAnalysis.CurrencyName);
                  Debug.Print("    Currency separator: " + swCostingAnalysis.CurrencySeparator);
                  Debug.Print("    Total manufacturing cost: " + swCostingAnalysis.GetManufacturingCost());
                  Debug.Print("    Material costs: " + swCostingAnalysis.GetMaterialCost());
                  Debug.Print("    Setup cost: " + swCostingAnalysis.GetSetupCost());
                  Debug.Print("    Total cost to charge: " + swCostingAnalysis.GetTotalCostToCharge());
                  Debug.Print("    Total cost to manufacture: " + swCostingAnalysis.GetTotalCostToManufacture());
                  Debug.Print("    Lot size: " + swCostingAnalysis.LotSize);
                  Debug.Print("    Total quantity: " + swCostingAnalysis.TotalQuantity);
                  Debug.Print("");
      
                  // Get Costing bodies
                  nbrCostingBodies = swCostingPart.GetBodyCount();
                  if ((nbrCostingBodies > 0))
                  {
                      Debug.Print("Costing bodies:");
                      Debug.Print("    Number of Costing bodies: " + nbrCostingBodies);
                      costingBodies = (object[])swCostingPart.GetBodies();
                      for (i = 0; i <= (nbrCostingBodies - 1); i++)
                      {
                          swCostingBody = (CostBody)costingBodies[i];
                          costingBodyName = swCostingBody.GetName();
                          Debug.Print("    Name of Costing body: " + costingBodyName);
                          // Make sure body is machining body
                          if ((swCostingBody.GetBodyType() == (int)swcBodyType_e.swcBodyType_Machined))
                          {
                              isBody = true;
                              // Determine analysis status of Costing body
                              switch ((int)swCostingBody.BodyStatus)
                              {
                                  case (int)swcBodyStatus_e.swcBodyStatus_NotAnalysed:
                                      // Create Costing analysis
                                      swCostingAnalysis = (CostAnalysis)swCostingBody.CreateCostAnalysis("c:\\program files\\solidworks corp\\solidworks\\lang\\english\\costing templates\\machiningtemplate_default(englishstandard).sldcts");
                                      Debug.Print("    Creating machining Costing analysis for: " + swCostingBody.GetName());
                                      break;
                                  case (int)swcBodyStatus_e.swcBodyStatus_Analysed:
                                      // Get Costing analysis
                                      swCostingAnalysis = (CostAnalysis)swCostingBody.GetCostAnalysis();
                                      Debug.Print("    Getting machining Costing analysis for: " + swCostingBody.GetName());
                                      break;
                                  case (int)swcBodyStatus_e.swcBodyStatus_Excluded:
                                      // Body excluded from Costing analysis
                                      Debug.Print("    Excluded from machining Costing analysis: " + swCostingBody.GetName());
                                      break;
                                  case (int)swcBodyStatus_e.swcBodyStatus_AssignedCustomCost:
                                      // Body has an assigned custom Cost
                                      Debug.Print("    Custom cost assigned: " + swCostingBody.GetName());
                                      break;
                              }
      
                              Debug.Print("");
      
                          }
                      }
                  }
      
                  if (!isBody)
                  {
                      Debug.Print("");
                      Debug.Print("No bodies in part! Exiting macro.");
                      return;
                  }
      
                  // Get machining Costing Analysis data
                  swCostingMachining = (CostAnalysisMachining)swCostingAnalysis.GetSpecificAnalysis();
                  Debug.Print("Machining Costing analysis: ");
                  Debug.Print("    Current material: " + swCostingMachining.CurrentMaterial);
                  Debug.Print("    Current material class: " + swCostingMachining.CurrentMaterialClass);
                  Debug.Print("    Current plate thickness: " + swCostingMachining.CurrentPlateThickness);
      
                  Debug.Print("");
      
                  // Get Costing features
                  Debug.Print("Costing features:");
                  swCostingFeat = (CostFeature)swCostingAnalysis.GetFirstCostFeature();
                  while ((swCostingFeat != null))
                  {
                      Debug.Print("    Feature: " + swCostingFeat.Name);
                      Debug.Print("      Type: " + swCostingFeat.GetType());
                      Debug.Print("        Setup related: " + swCostingFeat.IsSetup);
                      Debug.Print("        Overridden: " + swCostingFeat.IsOverridden);
                      Debug.Print("        Combined cost: " + swCostingFeat.CombinedCost);
                      Debug.Print("        Combined time: " + swCostingFeat.CombinedTime);
      
                      swCostingSubFeat = swCostingFeat.GetFirstSubFeature();
                      while ((swCostingSubFeat != null))
                      {
                          Debug.Print("      Subfeature: " + swCostingSubFeat.Name);
                          Debug.Print("        Type: " + swCostingSubFeat.GetType());
                          Debug.Print("          Setup related: " + swCostingSubFeat.IsSetup);
                          Debug.Print("          Overridden: " + swCostingSubFeat.IsOverridden);
                          Debug.Print("          Combined cost: " + swCostingSubFeat.CombinedCost);
                          Debug.Print("          Combined time: " + swCostingSubFeat.CombinedTime);
                          swCostingNextSubFeat = (CostFeature)swCostingSubFeat.GetNextFeature();
                          swCostingSubFeat = null;
                          swCostingSubFeat = (CostFeature)swCostingNextSubFeat;
                          swCostingNextSubFeat = null;
                      }
                      swCostingNextFeat = swCostingFeat.GetNextFeature();
                      swCostingFeat = null;
                      swCostingFeat = (CostFeature)swCostingNextFeat;
                      swCostingNextFeat = null;
                  }
      
              }
          }
      }