C# SolidWorks 二次开发 API --- 实例:多实体零件导出

这一篇文章来谈一下如何快速导出一个零件中的多实体,每个实体以单独零件名,并且会简单的得到它的包围盒信息,

最终生成一个装配体。

 

 下面是零件: 以多实体进行的设计,实体并且设定了材质。

 程序会在同目录下生成相同的文件夹和装配体信息, 有了装配体,后面就方便生成BOM了(同时程序中对程序的复制 阵列做了)处理.

 获取实体关键代码:

 private void ListBodies()
        {
            ModelDoc2 swModel = null;
            PartDoc swPart = null;
            object vBody;
            bool bRet;

            swModel = (ModelDoc2)iswApp.ActiveDoc;
            swModel.ClearSelection2(true);
            Debug.Print("File = " + swModel.GetPathName());

            txtPath.Text = swModel.GetPathName();

            this.Text = System.IO.Path.GetFileName(txtPath.Text);

            switch (swModel.GetType())
            {
                case (int)swDocumentTypes_e.swDocPART:
                    swPart = (PartDoc)swModel;
                    // Solid bodies
                    object[] vBodyArr = null;
                    Body2 swBody = default(Body2);

                    MathTransform swMathTrans = null;
                    vBodyArr = (object[])swPart.GetBodies2((int)swBodyType_e.swSolidBody, true);

                    if ((vBodyArr != null))
                    {
                        // Debug.Print("  Number of solid bodies: " + vBodyArr.Length);

                        foreach (object vBody_loopVariable in vBodyArr)
                        {
                            vBody = vBody_loopVariable;
                            swBody = (Body2)vBody;

                            string[] vConfigName = null;
                            vConfigName = (string[])swModel.GetConfigurationNames();
                            string sMatDB = "";
                            string sMatName = swBody.GetMaterialPropertyName("", out sMatDB);

                            //bRet = swBody.RemoveMaterialProperty((int)swInConfigurationOpts_e.swAllConfiguration, (vConfigName));

                            // Debug.Print("Body--> " + swBody.Name + " " + "");

                            FeatureType Ftype = 0;

                            var childFeature = swBody.GetFeatures();

                            foreach (var item in childFeature)
                            {
                                Feature f = (Feature)item;

                                Debug.Print(swBody.Name + "-->" + f.GetTypeName());

                                if (f.GetTypeName() == "MoveCopyBody")
                                {
                                    Ftype = FeatureType.Copy;
                                }
                                if (f.GetTypeName() == "MirrorSolid")
                                {
                                    Ftype = FeatureType.Mirror;
                                }
                            }

                            Body2 swOriBody = null;

                            string swOriBodyName = "";
                            string swOriBodyBox = "";

                            if (Ftype != 0)
                            {
                                try
                                {
                                    swOriBody = swBody.GetOriginalPatternedBody(out swMathTrans);

                                    swOriBodyName = swOriBody.Name;

                                    swOriBodyBox = GetBodyBox(swOriBody);
                                }
                                catch (Exception)
                                {
                                }
                            }

                            string bbox = GetBodyBox(swBody);

                            BodyModel tempBodyM = new BodyModel(swBody.Name, sMatName, swOriBodyName, Ftype, bbox);

                            if (bbox == swOriBodyBox && swBody.Name.ToString().Contains(swOriBodyName.ToString()) == false)
                            {
                                if ((int)tempBodyM.featureT == 0)
                                {
                                    listBodies_Normally.Items.Add(tempBodyM.name);
                                }
                                else if ((int)tempBodyM.featureT == 1 && tempBodyM.name.Contains("镜向") == true) //mirror
                                {
                                    listBodies_MirrorCopy.Items.Add(tempBodyM.name + "<--M--" + tempBodyM.refBodyname);
                                    tempBodyM.comment = "镜向-" + tempBodyM.refBodyname;
                                    //tempBodyM.name = "镜像-" + tempBodyM.refBodyname + "-" ;
                                }
                                else if ((int)tempBodyM.featureT == 2 && (tempBodyM.name.Contains("复制") == true || tempBodyM.name.Contains("阵列") == true))  //copy
                                {
                                    listBodies_MirrorCopy.Items.Add(tempBodyM.name + "<--C--" + tempBodyM.refBodyname);

                                    tempBodyM.comment = "复制-" + tempBodyM.refBodyname;
                                }
                            }
                            else
                            {
                                listBodies_Normally.Items.Add(tempBodyM.name);
                            }

                            bodyModels.Add(tempBodyM);
                        }
                    }
                    break;

                case (int)swDocumentTypes_e.swDocASSEMBLY:
                    //ProcessAssembly(swApp, swModel);
                    break;

                default:
                    return;
                    break;
            }

            Debug.Print(bodyModels.Count.ToString());
        }

生成新的装配体代码:

    private void CreateNewAssembly(string assemblyName, List<string> partsname)
        {
            string assemblyDefaultPath = iswApp.GetDocumentTemplate(2, "", 0, 0, 0);

            var part = iswApp.NewDocument(assemblyDefaultPath, 0, 0, 0);

            if (part != null)
            {
                AssemblyDoc assemblyDoc = part as AssemblyDoc;

                ModelDoc2 modelDoc2 = assemblyDoc as ModelDoc2;

                ModelDocExtension swModExt = default(ModelDocExtension);

                int errors = 0;
                int warnings = 0;

                swModExt = (ModelDocExtension)modelDoc2.Extension;

                swModExt.SaveAs(assemblyName,
                    (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, null, errors, warnings);

                modelDoc2 = iswApp.ActiveDoc;

                int i = 0;
                int tempV = ProgressBar.Value;
                foreach (var partN in partsname)
                {
                    labStatus.Text = "正在装配-->" + Path.GetFileNameWithoutExtension(partN);

                    ProgressBar.Value = tempV + i;

                    iswApp.OpenDoc6(partN.ToString(), 1, 32, "", ref errors, ref warnings);

                    assemblyDoc = (AssemblyDoc)iswApp.ActivateDoc3(System.IO.Path.GetFileNameWithoutExtension(assemblyName) + ".sldasm", true, 0, errors);

                    Component2 swInsertedComponent = default(Component2);

                    swInsertedComponent = assemblyDoc.AddComponent5(partN, 0, "", false, "", 0, 0, 0);

                    modelDoc2 = iswApp.ActiveDoc;
                    modelDoc2.ClearSelection2(true);

                    modelDoc2.Extension.SelectByID2(swInsertedComponent.GetSelectByIDString(), "COMPONENT", 0, 0, 0, false, 0, null, 0);

                    assemblyDoc.UnfixComponent();

                    modelDoc2.ClearSelection2(true);

                    modelDoc2.Extension.SelectByID2("Point1@Origin@" + swInsertedComponent.GetSelectByIDString(), "EXTSKETCHPOINT", 0, 0, 0, false, 0, null, 0);

                    modelDoc2.Extension.SelectByID2("Point1@Origin", "EXTSKETCHPOINT", 0, 0, 0, true, 0, null, 0);

                    Mate2 mate2 = default(Mate2);

                    mate2 = assemblyDoc.AddMate5(20, -1, false, 0, 0, 0, 0, 0.001, 0, 0, 0, false, false, 0, out int warings);
                    modelDoc2.ClearSelection2(true);
                    modelDoc2.EditRebuild3();

                    iswApp.CloseDoc(partN);

                    i = i + 1;
                }

                iswApp.ActivateDoc3(System.IO.Path.GetFileNameWithoutExtension(assemblyName) + ".sldasm", true, 0, errors);
                modelDoc2 = iswApp.ActiveDoc;
                modelDoc2.ShowNamedView2("*等轴测", 7);
                modelDoc2.ViewZoomtofit2();
                modelDoc2.Save();
            }

            ProgressBar.Value = ProgressBar.Maximum;
        }

完整代码请去码云或者github.

https://gitee.com/painezeng/CSharpAndSolidWorks

https://github.com/painezeng/CSharpAndSolidWorks

发布了51 篇原创文章 · 获赞 17 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zengqh0314/article/details/103560570