[英]Macro to export Solidworks Part to dxf
I want to export my part as dxf as part of a macro.我想将我的零件导出为 dxf 作为宏的一部分。 I tried recording it, but when I execute the recording it runs into erroors (see below).
我尝试录制它,但是当我执行录制时它遇到了错误(见下文)。 Thus I took it to the inte.net and tried the answer provided to this question "Macro for Saving Solidworks part configurations as dxf files" , but it doesn't seem to work for me either.
因此我把它带到了 inte.net 并尝试了这个问题“用于将 Solidworks 零件配置保存为 dxf 文件的宏”的答案,但它似乎对我也不起作用。
I have tried exporting to other formats, such as step, xt and stl, which all work flawlessly.我试过导出为其他格式,例如 step、xt 和 stl,它们都可以正常工作。 The macro for dxf export though executes without any errors, but it also does not export/save anything.
dxf 导出的宏虽然执行时没有任何错误,但它也不会导出/保存任何内容。 Trying to use this with Solidworks2017
尝试将其与 Solidworks2017 一起使用
I have also tried to record my own macro but it stops at:我也尝试录制自己的宏,但它停在:
Set myView = Part.CreateDrawViewFromModelView3(Part, "*Oben", 0, 0, 0)
with the errormsg:与错误消息:
Runtime error '438': object does not support this property or method.
运行时错误“438”:object 不支持此属性或方法。
here the recorded macro in full:这里是完整的录制宏:
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Dim COSMOSWORKSObj As Object
Dim CWAddinCallBackObj As Object
Set CWAddinCallBackObj = swApp.GetAddInObject("CosmosWorks.CosmosWorks")
Set COSMOSWORKSObj = CWAddinCallBackObj.COSMOSWORKS
Dim FilePath As String
Dim PathSize As Long
Dim PathNoExtention As String
Dim NewFilePath As String
FilePath = Part.GetPathName
PathSize = Strings.Len(FilePath)
PathNoExtention = Strings.Left(FilePath, PathSize - 6)
NewFilePath = PathNoExtention & "dxf"
' Save As
longstatus = Part.SaveAs3(NewFilePath, 0, 0)
' Redraw
Part.GraphicsRedraw2
Set Part = swApp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView
myModelView.FrameLeft = 0
myModelView.FrameTop = 22
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
Set Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2017\templates\Zeichnung.drwdot", 0, 0, 0)
swApp.ActivateDoc2 "Zeichnung5 - Blatt1", False, longstatus
Set Part = swApp.ActiveDoc
Set Part = swApp.ActiveDoc
Set myModelView = Part.ActiveView
myModelView.FrameLeft = 0
myModelView.FrameTop = 0
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
swApp.ActivateDoc2 FilePath, False, longstatus
Set Part = swApp.ActiveDoc
Dim myView As Object
Set myView = Part.CreateDrawViewFromModelView3(Part, "*Oben", 0, 0, 0)
Set myView = Part.CreateDrawViewFromModelView3(Part, "*Normal auf", 0, 0, 0)
Part.ClearSelection2 True
Part.ClearSelection2 True
End Sub
Exporting to DXF (or DWG) is different to saving other formats from SolidWorks as you have found out.正如您所发现的,导出为 DXF(或 DWG)不同于从 SolidWorks 保存其他格式。 It's typically used to export flat patterns of sheet metal components.
它通常用于导出钣金组件的展开模式。
ExportToDWG2 (IPartDoc)
is the API method you need to call to export to either of these two formats. ExportToDWG2 (IPartDoc)
是您需要调用以导出为这两种格式之一的 API 方法。 As per the docs it allows you to:根据文档,它允许您:
Saves various aspects of a part (sheet metal, faces, loops, and annotation views) to one or more DXF/DWG files'
将零件的各个方面(钣金、面、环和注释视图)保存到一个或多个 DXF/DWG 文件中
As such how you use this call will vary based on what you want to export.因此,您使用此调用的方式将根据您要导出的内容而有所不同。
If you are exporting sheet metal, as per the example you linked, then you will output a flat pattern.如果您要导出钣金件,按照您链接的示例,那么您将 output 一个展开模式。 However if you try to export sheet metal with a model that does not contain any sheet metal features then the result will be that the call returns
false
and nothing is output.但是,如果您尝试使用不包含任何钣金特征的 model 导出钣金,则结果将是调用返回
false
并且没有任何内容是 output。
The following would be suitable for a sheet metal part:以下适用于钣金零件:
Dim success As Boolean
success = swPart.ExportToDWG2(dxfFilePath, modelFilePath, swExportToDWG_e.swExportToDWG_ExportSheetMetal, True, Nothing, False, False, sheetMetalOptions, Nothing)
There is no enum in SolidWorks for the sheet metal options but here's one you could use: SolidWorks 中没有针对钣金选项的枚举,但您可以使用以下选项:
Enum SheetMetalOptions_e
None = 0
Geometry = 1
HiddenEdges = 2
BendLines = 4
Sketches = 8
CoplanarFaces = 16
LibraryFeatures = 32
FormingTools = 64
BoundingBox = 2048
End Enum
Usage would be as follows:用法如下:
' Export geometry, hidden edges and bend lines
Dim sheetMetalOptions As SheetMetalOptions_e
sheetMetalOptions = Geometry Or HiddenEdges Or BendLines
A full macro may look like the below:完整的宏可能如下所示:
Enum SheetMetalOptions_e
None = 0
Geometry = 1
HiddenEdges = 2
BendLines = 4
Sketches = 8
CoplanarFaces = 16
LibraryFeatures = 32
FormingTools = 64
BoundingBox = 2048
End Enum
Sub Main()
' Connect to SolidWorks
Dim swApp As SldWorks.SldWorks
Set swApp = Application.SldWorks
' Connect to the active model
Dim swModel As ModelDoc2
Set swModel = swApp.ActiveDoc
' Validate a model is open
If swModel Is Nothing Then
swApp.SendMsgToUser2 "Open a part to run this macro", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
Exit Sub
End If
' Validate the open model is a part document
If swModel.GetType <> swDocumentTypes_e.swDocPART Then
swApp.SendMsgToUser2 "This macro only runs on part documents", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
Exit Sub
End If
Dim swPart As PartDoc
Set swPart = swModel
' Build the new file path
Dim filePath As String
Dim pathSize As Long
Dim pathNoExtention As String
Dim newFilePath As String
filePath = swModel.GetPathName 'WARNING: this will be an empty string if the part document has not been saved
pathSize = Strings.Len(filePath)
pathNoExtention = Strings.Left(filePath, pathSize - 6) 'WARNING: this assumes the file extension is 6 characters (sldprt)
newFilePath = pathNoExtention & "dxf"
' Define sheet metal information to export
Dim sheetMetalOptions As SheetMetalOptions_e
sheetMetalOptions = Geometry Or HiddenEdges Or BendLines
' Export the DXF
Dim success As Boolean
success = swPart.ExportToDWG2(newFilePath, filePath, swExportToDWG_e.swExportToDWG_ExportSheetMetal, True, Nothing, False, False, 0, Nothing)
' Report success or failure to the user
If success Then
swApp.SendMsgToUser2 "The DXF was exported successfully", swMessageBoxIcon_e.swMbInformation, swMessageBoxBtn_e.swMbOk
Else
swApp.SendMsgToUser2 "Failed to export the DXF", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
End If
End Sub
If your part does not contain sheet metal features then you will have to use one of the alternative export actions: Selected Faces or Loops
or Annotation Views
.如果您的零件不包含钣金特征,那么您将不得不使用其中一种替代导出操作:
Selected Faces or Loops
或Annotation Views
。
Annotation Views
is supposed to let you pass in an array of view names (such as *Front
or *Current
) but in my experience it does not work reliably and I am aware that there are some SPRs open against this in SolidWorks. Annotation Views
应该让您传递一组视图名称(例如*Front
或*Current
),但根据我的经验,它不能可靠地工作,而且我知道在 SolidWorks 中有一些 SPR 反对这个。
Selected Faces or Loops
requires you to pre-select a face or loop prior to calling ExportToDWG2
, so you would have to write some code to determine which faces or loop you want to use and select it, then call ExportToDWG2
with the action
parameter set to swExportToDWG_e.swExportToDWG_ExportSelectedFacesOrLoops
. Selected Faces or Loops
要求您在调用ExportToDWG2
之前预先选择一个面或循环,因此您必须编写一些代码来确定要使用哪些面或循环,然后 select 它,然后调用ExportToDWG2
并将action
参数设置为swExportToDWG_e.swExportToDWG_ExportSelectedFacesOrLoops
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.