Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drawing Export Macro

Status
Not open for further replies.

bsj0526

Mechanical
Jan 5, 2008
40
Hello,

I am trying to write a macro that will help speed up the process of constantly saving drawings as dxf's for the machine shop. Here is what I have so far:

Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Dim CPM As SldWorks.CustomPropertyManager

Sub main()

Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set CPM = Part.Extension.CustomPropertyManager("")

Dim FilePath As String
Dim PathSize As Long
Dim PathNoExtension As String
Dim NewFilePath As String
Dim MyRevVal As String
Dim MyRevValEvaluated As String

CPM.Get2 "Revision", MyRevVal, MyRevValEvaluated

FilePath = Part.GetPathName & Revision
PathSize = Strings.Len(FilePath)
PathNoExtension = Strings.Left(FilePath, PathSize - 6)
NewFilePath = PathNoExtension & MyRevValEvaluated & "dxf"

Part.SaveAs2 NewFilePath, 0, True, False
MsgBox "File Exported " & NewFilePath
End Sub

I have two problems.

1) It exports the file as "filename.RevDXF
File name being the drawing's name and the revision being the custom property value. Obviously the "." needs to be 3 characters over to the right.

2) The macro gives the dxf the revision of the drawing file, not the part. When we rev a part, we update the revision of the part file, not the drawing. I would like the macro to get the revision from the part file or one of the drawing views.

Any help is greatly appreciated.
 
Replies continue below

Recommended for you

Code:
PathNoExtension = Strings.Left(FilePath, PathSize - [!]7[/!])
NewFilePath = PathNoExtension & MyRevValEvaluated & [!]".dxf"[/!]

[bat]Honesty may be the best policy, but insanity is a better defense.[bat]
-SolidWorks API VB programming help
 
To obtain the custom properties of the model shown in the drawing, you have to first have your macro figure out what model is shown in the drawing. You can then get the custom property manager for that ModelDoc2 and get the revision custom property from that. Here's a sample macro that will look at a drawing and get the referenced document. The While loop is necessary because the first view or two of a drawing document are not actually model views, they are the sheet format etc, which do not have referenced documents. GetNextView will return the next view of the document until it gets to the last view, at which point it will return Nothing. This code will stop at the first view it finds that has a referenced document. Of course, if your drawing file references multiple different parts/assemblies (even if they are on different sheets), you will have to dig a little deeper.

Code:
Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim myRefDoc As SldWorks.ModelDoc2

Sub main()
Set swApp = Application.SldWorks

If swApp.ActiveDoc.GetType <> swDocDRAWING Then
    MsgBox "Not a drawing"
    Exit Sub
End If

Set swDoc = swApp.ActiveDoc
Set swView = swDoc.GetFirstView
While (Not (swView Is Nothing)) And (myRefDoc Is Nothing)
    If swView.ReferencedDocument Is Nothing Then
        Set swView = swView.GetNextView
    Else
        Set myRefDoc = swView.ReferencedDocument
    End If
Wend
If myRefDoc Is Nothing Then
    MsgBox "No referenced document found."
Else
    MsgBox "Referenced document: " & myRefDoc.GetPathName
End If
End Sub
 
Thanks for the help. I think I should be able to take it from here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor