[英]Macro to export Solidworks Part to dxf
我想將我的零件導出為 dxf 作為宏的一部分。 我嘗試錄制它,但是當我執行錄制時它遇到了錯誤(見下文)。 因此我把它帶到了 inte.net 並嘗試了這個問題“用於將 Solidworks 零件配置保存為 dxf 文件的宏”的答案,但它似乎對我也不起作用。
我試過導出為其他格式,例如 step、xt 和 stl,它們都可以正常工作。 dxf 導出的宏雖然執行時沒有任何錯誤,但它也不會導出/保存任何內容。 嘗試將其與 Solidworks2017 一起使用
我也嘗試錄制自己的宏,但它停在:
Set myView = Part.CreateDrawViewFromModelView3(Part, "*Oben", 0, 0, 0)
與錯誤消息:
運行時錯誤“438”:object 不支持此屬性或方法。
這里是完整的錄制宏:
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
正如您所發現的,導出為 DXF(或 DWG)不同於從 SolidWorks 保存其他格式。 它通常用於導出鈑金組件的展開模式。
ExportToDWG2 (IPartDoc)
是您需要調用以導出為這兩種格式之一的 API 方法。 根據文檔,它允許您:
將零件的各個方面(鈑金、面、環和注釋視圖)保存到一個或多個 DXF/DWG 文件中
因此,您使用此調用的方式將根據您要導出的內容而有所不同。
如果您要導出鈑金件,按照您鏈接的示例,那么您將 output 一個展開模式。 但是,如果您嘗試使用不包含任何鈑金特征的 model 導出鈑金,則結果將是調用返回false
並且沒有任何內容是 output。
以下適用於鈑金零件:
Dim success As Boolean
success = swPart.ExportToDWG2(dxfFilePath, modelFilePath, swExportToDWG_e.swExportToDWG_ExportSheetMetal, True, Nothing, False, False, sheetMetalOptions, Nothing)
SolidWorks 中沒有針對鈑金選項的枚舉,但您可以使用以下選項:
Enum SheetMetalOptions_e
None = 0
Geometry = 1
HiddenEdges = 2
BendLines = 4
Sketches = 8
CoplanarFaces = 16
LibraryFeatures = 32
FormingTools = 64
BoundingBox = 2048
End Enum
用法如下:
' Export geometry, hidden edges and bend lines
Dim sheetMetalOptions As SheetMetalOptions_e
sheetMetalOptions = Geometry Or HiddenEdges Or BendLines
完整的宏可能如下所示:
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
如果您的零件不包含鈑金特征,那么您將不得不使用其中一種替代導出操作: Selected Faces or Loops
或Annotation Views
。
Annotation Views
應該讓您傳遞一組視圖名稱(例如*Front
或*Current
),但根據我的經驗,它不能可靠地工作,而且我知道在 SolidWorks 中有一些 SPR 反對這個。
Selected Faces or Loops
要求您在調用ExportToDWG2
之前預先選擇一個面或循環,因此您必須編寫一些代碼來確定要使用哪些面或循環,然后 select 它,然后調用ExportToDWG2
並將action
參數設置為swExportToDWG_e.swExportToDWG_ExportSelectedFacesOrLoops
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.