SolidWorks二次开发 API-单独导入某个2d图层数据

SolidWorks二次开发 API-单独导入某个2d图层数据

之前在第53个功能中讲过了如何直接导入dxf文件到solidworks的草图中。
今天我们来讲一下如何导入某个图层上的数据,这两个功能所使用的api有区别。
如下图,cad中有三个图层,每个层里有一个形状。

现在我们用代码把图层2 导入到solidworks中。

  var swApp = PStandAlone.GetSolidWorks();

            string partDefaultTemplate = swApp.GetDocumentTemplate((int)swDocumentTypes_e.swDocPART, "", 0, 0, 0);
            
            var newDoc = swApp.NewDocument(partDefaultTemplate, 0, 0, 0);
                        
            var swModel = (ModelDoc2)swApp.ActiveDoc;

            var actPath = RegDllPath("");

            var start = actPath.Substring(0, actPath.IndexOf("CSharpAndSolidWorks", 0));        

            var dwgPath = $@"{start}CSharpAndSolidWorks\CSharpAndSolidWorks\TemplateModel\ImportDWG.dwg";

            bool boolstatus = swModel.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, false, 0, null, 0);
            if (!boolstatus)
            {
                 boolstatus = swModel.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, false, 0, null, 0);

                if (!boolstatus)
                {
                    boolstatus = swModel.Extension.SelectByID2("Front", "PLANE", 0, 0, 0, false, 0, null, 0);

                    if (!boolstatus)
                    {
                        boolstatus = swModel.Extension.SelectByID2("Plane1", "PLANE", 0, 0, 0, false, 0, null, 0);

                        if (!boolstatus)
                        {
                            MessageBox.Show("请选中一个基准面,再点确定。");

                        }           
                        
                    }
                }
            }
         
                       

            ImportDxfDwgData importData = default(ImportDxfDwgData);

            importData = (ImportDxfDwgData)swApp.GetImportFileData(dwgPath);

            importData.set_LengthUnit("", (int)swLengthUnit_e.swMM);
                       
            var bRet = importData.SetPosition("", (int)swDwgImportEntitiesPositioning_e.swDwgEntitiesCentered, 0, 0);
               
            importData.set_ImportMethod("", (int)swImportDxfDwg_ImportMethod_e.swImportDxfDwg_ImportToExistingPart);

            importData.SetMergePoints("", true, 0.001);

            importData.SetImportLayerVisibility(null, (int)swImportDxfDwg_LayerVisibility_e.swImportDxfDwg_LayerHidden);

            var listLayerNames = new List<string>() { "2" };
            var tempArray = listLayerNames.ToArray();
            object layers = tempArray;
            importData.SetImportLayerVisibility(layers, (int)swImportDxfDwg_LayerVisibility_e.swImportDxfDwg_LayerVisible);
               
            var LayFea01 = swModel.FeatureManager.InsertDwgOrDxfFile2(dwgPath, importData);

            if (LayFea01!=null)
            {
                LayFea01.Name = "导入的图层2";
            }
   

执行完成,结果如下图:

是不是很完美,本文结束。我要去看球赛了。

posted @
2022-11-29 19:00 
painezeng  阅读(
0)  评论(
0
编辑 
收藏 
举报  
来源