×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Drawing Export Macro

Drawing Export Macro

Drawing Export Macro

(OP)
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.

RE: Drawing Export Macro

CODE

PathNoExtension = Strings.Left(FilePath, PathSize - 7)
NewFilePath = PathNoExtension & MyRevValEvaluated & ".dxf"

batHonesty may be the best policy, but insanity is a better defense.bat
http://www.EsoxRepublic.com-SolidWorks API VB programming help

RE: Drawing Export Macro

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

RE: Drawing Export Macro

(OP)
Thanks for the help.  I think I should be able to take it from here.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources