AnsweredAssumed Answered

Solidworks C# API set Depth to Extruded

Question asked by Alex Alex on Feb 21, 2019
Latest reply on Feb 27, 2019 by Alex Alex

Hello friends.

I have an assembly model in Solidworks.

I use API C# to Solidworks for automatic create a model.

In a part, I need to set Depth 10 or 40 mm.

Not a single example on the forum helped me.

        static SldWorks swApp;

        static AssemblyDoc assemblyDoc;

        static ModelDoc2 assemblyModel;

        static ModelDocExtension swModelDocExt;

        static FeatureManager swFeatureManager;

        static EquationMgr assemblyMgr;

        static IDimension partDimension;

        static Feature swFeature; 

        static IModelDoc2 partModel;

        static ExtrudeFeatureData2 swExtrudeFeatData;

 

public static void getComponents()

        {

            Console.WriteLine("getComponents");

           

            Process[] processes = Process.GetProcessesByName("SLDWORKS");

            foreach (Process process in processes)

            {

                Console.WriteLine("kill process ");

                process.CloseMainWindow();

                process.Kill();

            }

    

            object processSW = System.Activator.CreateInstance(System.Type.GetTypeFromProgID("SldWorks.Application"));

            swApp = (SldWorks)processSW;

            swApp.Visible = true;

            //open file

            int fileError = 0, fileWarning = 0;

            string pathToFileAssembly = "C:\\Users\\administrator\\Desktop\\SW\\AssemBelt.SLDASM";

            assemblyModel = swApp.OpenDoc6(pathToFileAssembly, (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "1", ref fileError, ref fileWarning);

 

            assemblyDoc = (AssemblyDoc)assemblyModel;

            assemblyMgr = assemblyModel.GetEquationMgr();

 

            swModelDocExt = (ModelDocExtension)assemblyModel.Extension;

            swFeatureManager = (FeatureManager)assemblyModel.FeatureManager;

 

            object[] comps = assemblyDoc.GetComponents(true);

            Console.WriteLine("Solidworks comps size = " + comps.Length);

            foreach (Component2 icomp in comps)

            {

                partModel = swApp.OpenDoc6(icomp.GetPathName(), (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "1", ref fileError, ref fileWarning);

   

                switch (icomp.Name)

                {

                    case "Part-1":  

                       ...

                        break;

 

                    case "Part-2":     

                       ...

                        break;

 

                    case "Part-3":       

                     

                        if (true)

                        {

                            partDimension = (Dimension)partModel.Parameter("hiegth@Sketch4");

                            partDimension.Value = 120;

 

                            partDimension = (Dimension)partModel.Parameter("thickness@Sketch4");

                            partDimension.Value = 5;

   

                            //THIS DOES NOT WORK

                            PartDoc doc = (PartDoc)partModel; 

                            swFeature = doc.FeatureByName("Base-Extrude");

                            swExtrudeFeatData = swFeature.GetDefinition();

                            swExtrudeFeatData.SetDepth(true, 0.04);     //or 10 mm or 40 mm

                            swFeature.ModifyDefinition(swExtrudeFeatData, partModel, null);

 

                            icomp.SetSuppression(1);

                            icomp.ReferencedConfiguration = "type-B";

                        }

                        else icomp.SetSuppression(0);

 

                        break;

                }

                Console.WriteLine(icomp.Name + " => " + icomp.GetPathName() + " => " + partModel);

            }//for end

        }

 

 

On this line, the program breaks down:

  swExtrudeFeatData = swFeature.GetDefinition();

 

swFeature equals NULL.

 

Thanks!

Outcomes