我认为这个插入同名DWG为草图的宏更实用。
- ' macro recorded on 05/28/16 by pyczt
- ' Preconditions:条件
- ' 1. Open a part.打开一个零件
- ' 2. Replace path_name with the pathname of an existing DXF/DWG file.同路径下有同名DWG文件
- ' 3. Select a plane or face on which to insert the DXF/DWG image.选择面
- '
- ' Postconditions:
- ' 1. The DXF/DWG image is added as a sketch. 自动插入DWG,形成草图
- '如果有单位问题,可以方法2进行,分解图块问题还没解决,单位改了,块的位置有可能有问题
- ' ******************************************************************************
- Dim swApp As SldWorks.SldWorks
- Dim swModel As SldWorks.ModelDoc2
- Dim sPathName As String
- Dim DxfDwgImportData As SldWorks.ImportDxfDwgData
- Sub main()
- Set swApp = Application.SldWorks
- Set swModel = swApp.ActiveDoc '设定当前SW文档
- sPathName = swModel.GetPathName '读取当前SW文档名(含路径)
- sPathName = Left(sPathName, Len(sPathName) - 6) + "dwg" '提取当前SW文件名加后缀名
- '***********************************************************************************
- 'Set DxfDwgImportData = swApp.GetImportFileData(sPathName) 这些是InsertDwgOrDxfFile2中采用
- 'Unit单位设定
- ' DxfDwgImportData.LengthUnit("") = SwConst.swLengthUnit_e.swMM
- 'Position坐标点设定
- ' bRet = DxfDwgImportData.SetPosition("", swDwgEntitiesCentered, 0, 0)
- 'Sheet scale比例设定
- 'bRet = DxfDwgImportData.SetSheetScale("", 1#, 2#)
- 'Paper size图纸大小
- ' bRet = DxfDwgImportData.SetPaperSize("", SwConst.swDwgPaperSizes_e.swDwgPaperAsize, 0#, 0#)
- 'Import method : Import this sheet (layout) into an existing part输入方式
- 'DxfDwgImportData.ImportMethod("") = swImportDxfDwg_ImportMethod_e.swImportDxfDwg_ImportToExistingPart
- Dim myFeature As Object
- 'Set myFeature = swModel.FeatureManager.InsertDwgOrDxfFile2(sPathName, DxfDwgImportData)
- Set myFeature = swModel.FeatureManager.InsertDwgOrDxfFile(sPathName)
- swModel.SketchManager.InsertSketch True
- End Sub
复制代码 |