简体   繁体   中英

Macro to export Solidworks Part to dxf

I want to export my part as dxf as part of a macro. 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.

I have tried exporting to other formats, such as step, xt and stl, which all work flawlessly. The macro for dxf export though executes without any errors, but it also does not export/save anything. Trying to use this with 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.

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. 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. 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'

As such how you use this call will vary based on what you want to export.

Sheet Metal

If you are exporting sheet metal, as per the example you linked, then you will output a flat pattern. 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.

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:

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

Faces, Loops or Annotation Views

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 .

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM