Can someone help me out with the macro

Below is a simple macro to export the current drawing as a DXF file to N:\DXF. As you can see, it names the DXF "Draw1.dxf". How can I edit the macro to so the dxf filename is the same filename as the SolidWorks drawing, which is our part numbering system. Thanks in advance.

Dim swApp As Object Dim Part As Object Dim boolstatus As Boolean Dim longstatus As Long Dim Annotation As Object Dim Gtol As Object Dim DatumTag As Object Dim FeatureData As Object Dim Feature As Object Dim Component As Object Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc Part.SaveAs2 "N:\DXF\Draw1.DXF", 0, True, False End Sub

Reply to
SW Monkey
Loading thread data ...

This should work.............

Dim swApp As Object Dim Part As Object Dim boolstatus As Boolean Dim longstatus As Long Dim Annotation As Object Dim Gtol As Object Dim DatumTag As Object Dim FeatureData As Object Dim Feature As Object Dim Component As Object Dim PartTitle As String

Sub main()

Set swApp = Application.SldWorks Set Part = swApp.ActiveDoc

PartTitle = Part.GetTitle

Part.SaveAs2 "N:\DXF\" & PartTitle & ".DXF", 0, True, False End Sub

Reply to
Corey Scheich

After the PartTitle = Part.GetTitle line, please add a check to see if there is a period in the PartTitle string. This can exist depending on how your Explorer settings are configured (Show extensions for known file types - if unchecked, the extensions will exist in the PartTitle).

Best Regards,

-- Bob Hanson CEO Custom Programming Unlimited LLC SolidWorks Solution Partner in two categories (SW API training/consulting and SW PDM provider)

formatting link
website (Home of SW API Public Code snippets)

SolidWorks 2003 World User Conference Exhibitor Booth #407

Reply to
Robert V. Hanson

Dim swApp As Object Dim Part As Object dim partitle as string dim partitlelen as integer dim partName as string

Sub main() Set swApp = Application.SldWorks Set Part = swApp.ActiveDoc

partitle = part.gettitle partitlelen = len(partitle) partname = left(partitle,partitlelen - (partitlelen -7))

Part.SaveAs2 "N:\DXF\" & partname & ".DXF", 0, True, False

End Sub

Reply to
Sean Phillips

Thanks! Works great. One last question, is there a command to add to this that would scale the drawing view 1:1 automatically before exporting as a DXF?

Reply to
SW Monkey

Can we add a suffix to each part number while running this macro? In our case "-01" to identify the first configuration.

Thanks in advance.

Reply to
Peter F. Clarke

Dim swApp As Object Dim Part As Object dim partitle as string dim partitlelen as integer dim partName as string

Sub main()

Set swApp = Application.SldWorks Set Part = swApp.ActiveDoc

partitle = part.gettitle partitlelen = len(partitle) partname = left(partitle,partitlelen - (partitlelen -7))

'YOU CAN ADD A LINE LIKE THIS SUFFIX = "-1" Part.SaveAs2 "N:\DXF\" & partname & SUFFIX & ".DXF", 0, True, False

End Sub

If you need to get your config names

Dim Config As SldWorks.configuration

Set Config = Part.GetActiveConfiguration ConfigName = Config.Name Part.SaveAs2 "N:\DXF\" & partname & ConfigName & ".DXF", 0, True, False

Reply to
Corey Scheich

Thanks Corey!

Reply to
Peter F. Clarke

I couldnt get the 1:1 scale code to work. Maybe I placed it in the wrong part of the macro. Can you explain. Thanks.

I also wanted to know if its possible to close the drawing without saving after the DXF file has been created.

Thanks. :)

Reply to
SW Monkey

You have to rebuild after the 1:1

You have to do a rebuild you have finished scaling. Use the sub below. This way if you want .25 scale or 1 or 2 or 50 you can call it from another sub and specify the scale

Sub viewScale(MyScale As Double) Dim swView As SldWorks.View Dim swApp As SldWorks.SldWorks Dim Part As DrawingDoc

Set swApp = Application.SldWorks Set Part = swApp.ActiveDoc Set swView = Part.GetFirstView

'this while statement will traverse every view on the sheet

While Not swView Is Nothing swView.ScaleDecimal = MyScale Set swView = swView.GetNextView Wend Part.EditRebuild

End Sub

This should do it. I think that if you want to save you have to do a Part.Save before CloseDoc

Dim ModelDocument as SldWorks.ModelDoc2 Set ModelDocument = Part swApp.CloseDoc ModelDocument.GetTitle

Reply to
Corey Scheich

PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.