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