C# SolidWorks 二次开发 API—导入dxf/dwg到图纸或者零件草图

C# SolidWorks 二次开发 API—导入dxf/dwg到图纸或者零件草图

有些情况下我们需要把以前的2D图纸借用到3D中,以前先画2D的时候就是把2D图画好之后 ,选中一些元素,直接Ctrl+C 然后在Solidworks中Ctrl+V就可以了。好像尺寸是没有的。 今天我们来看下如何找api,以及实现这个功能。路子其实都是相通的,会找一个,后面的都会了。
关键字? 这里很明显就是Dxf 或者dwg
来吧,开始搜索。

api帮助跳出来的第一个就是dxf/dwg files
下面有几个小主题,我们我们看下,和我们的目标比较近的就是import或者load .


而且两个方法中都有实例给我们参考:
如果看不懂,就可以复制百度翻译一把: 这样就可以继续研究了:


接下来的步骤就差不多了,挑一个自己喜欢的语言版本的实例,去测试效果。
如下面这个,就是其中一个例子:

下面是api中的源版

SOLIDWORKS API Help
Insert and Position DXF/DWG File in Drawing Example (C#)     
This example shows how to insert and position a DXF/DWG file in a drawing. 

//---------------------------------------------------------------------------
// Preconditions:
// 1. Open a drawing.
// 2. Replace DXF_file_path with the pathname of an existing DXF/DWG file.
// 3. Open the Immediate window.
//
// Postconditions:
// 1. Inserts the DXF/DWG file as per the specified import data.
// 2. Inspect the Immediate window.
//---------------------------------------------------------------------------
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
namespace InsertDXFDrawing_CSharp.csproj
{
    partial class SolidWorksMacro
    {

        public void Main()
        {
            const string sDwgFileName = "DXF_file_path";

            ModelDoc2 swModel = default(ModelDoc2);
            ModelView swModelView = default(ModelView);
            DrawingDoc swDraw = default(DrawingDoc);
            FeatureManager swFeatMgr = default(FeatureManager);
            Feature swFeat = default(Feature);
            Sketch swSketch = default(Sketch);
            View swView = default(View);
            double[] vPos = null;
            bool bRet = false;
            ImportDxfDwgData importData = default(ImportDxfDwgData);

            swModel = (ModelDoc2)swApp.ActiveDoc;
            swModelView = (ModelView)swModel.ActiveView;

            bRet = swModel.Extension.SelectByID2("Sheet1", "SHEET", 0.0, 0.0, 0, false, 0, null, 0);

            swDraw = (DrawingDoc)swModel;
            swFeatMgr = swModel.FeatureManager;
            importData = (ImportDxfDwgData)swApp.GetImportFileData(sDwgFileName);

            // Unit
            importData.set_LengthUnit("", (int)swLengthUnit_e.swINCHES);

            // Position
            bRet = importData.SetPosition("", (int)swDwgImportEntitiesPositioning_e.swDwgEntitiesCentered, 0, 0);

            // Sheet scale
            bRet = importData.SetSheetScale("", 1.0, 2.0);

            // Paper size
            bRet = importData.SetPaperSize("", (int)swDwgPaperSizes_e.swDwgPaperAsize, 0.0, 0.0);

            //Import method
            importData.set_ImportMethod("", (int)swImportDxfDwg_ImportMethod_e.swImportDxfDwg_ImportToExistingDrawing);

            // Import file with importData
            swFeat = swFeatMgr.InsertDwgOrDxfFile2(sDwgFileName, importData);
            swSketch = (Sketch)swFeat.GetSpecificFeature2();

            swView = (View)swDraw.GetFirstView();

            while ((swView != null))
            {
                if (object.ReferenceEquals(swSketch, swView.GetSketch()))
                {
                    break; 
                }
                swView = (View)swView.GetNextView();
            }

            vPos = (double[])swView.Position;

            Debug.Print("File = " + swModel.GetPathName());
            Debug.Print(" Sketch = " + swFeat.Name);
            Debug.Print(" View = " + swView.Name);
            Debug.Print(" Old Pos = (" + vPos[0] * 1000.0 + ", " + vPos[1] * 1000.0 + ") mm");

            // Move to right
            vPos[0] = vPos[0] + 0.01;
            swView.Position = vPos;

            vPos = (double[])swView.Position;
            Debug.Print(" New Pos = (" + vPos[0] * 1000.0 + ", " + vPos[1] * 1000.0 + ") mm");

            // Redraw
            double[] rect = null;
            rect = null;
            swModelView.GraphicsRedraw(rect);

        }

        public SldWorks swApp;

    }
} 

这个我们用到我们的实例中,需要做一些修改。

首选把 swApp 引用改为实体,如我们最近系列中一直使用的
SldWorks swApp = PStandAlone.GetSolidWorks();
然后就是基本照抄模式。

下面我的代码是另一个例子的C#版本。

 /// <summary>
        /// 导入dxf到 sketch
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnImpotDxfToSketch_Click(object sender, EventArgs e)
        {
            SldWorks swApp = PStandAlone.GetSolidWorks();

            //确保文件存在
            string filename = @"C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2018\samples\tutorial\importexport\rainbow.DXF";

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

            importData.ImportMethod[""] = (int)swImportDxfDwg_ImportMethod_e.swImportDxfDwg_ImportToPartSketch;

            int longerrors = 0;

            var newDoc = swApp.LoadFile4(filename, "", importData, ref longerrors);

            //Gets
            Debug.Print("Part Sketch Gets:");
            Debug.Print(" Add constraints: " + importData.AddSketchConstraints[""]);
            Debug.Print(" Merge points: " + importData.GetMergePoints(""));
            Debug.Print(" Merge distance: " + (importData.GetMergeDistance("") * 1000));
            Debug.Print(" Import dimensions: " + importData.ImportDimensions[""]);
            Debug.Print(" Import hatch: " + importData.ImportHatch[""]);
            //Sets
            Debug.Print("Part Sketch Sets:");
            importData.AddSketchConstraints[""] = true;
            Debug.Print(" Add constraints: " + importData.AddSketchConstraints[""]);
            var retVal = importData.SetMergePoints("", true, 0.000002);
            Debug.Print(" Merge points: " + retVal);
            Debug.Print(" Merge distance: " + (importData.GetMergeDistance("") * 1000));
            importData.ImportDimensions[""] = true;
            Debug.Print(" Import dimensions: " + importData.ImportDimensions[""]);
            importData.ImportHatch[""] = false;
            Debug.Print(" Import hatch: " + importData.ImportHatch[""]);
        }

执行完之后 。solidworks中出现了传说中的彩虹!哈哈。。。


立即窗口中显示 了一些信息,后面有空继续研究!
代码已经上传.可在此下载源码:https://gitee.com/painezeng/CSharpAndSolidWorks

posted @
2023-03-21 23:08 
painezeng  阅读(
0)  评论(
0
编辑 
收藏 
举报  
来源