Jump to content

Welcome to ExtremeHW

Welcome to ExtremeHW, like most online communities you must register to view or post in our community, but don't worry this is a simple free process that requires minimal information for you to signup. Be a part of ExtremeHW by signing in or creating an account.

  • Start new topics and reply to others
  • Subscribe to topics and forums to get email updates
  • Get your own profile page and make new friends
  • Send personal messages to other members.
  • Take advantage of site exclusive features.
IGNORED

Solidwork Macro Troubles


ENTERPRISE
 Share

Recommended Posts

Hey all, 

 

So I am having a little bit of a fight with Solidwork Macros. Not sure if anyone here will be able to help, but lets have at it. 

 

So essentially I have two working individual Macro files. The Goal of the Macro files is to Save a document and then run an auto expert of the current open file into PDF and DWG. So I have two Macro files aptly named:

 

1.Save As 

2.PDF & DWG Export 

 

Initially I tried having it as one combined Macro, but could not get it to play nicely. The I realised there is a Group function for VBA Macros. Which is a Macro that you can used to launch other Macros in sequence. Alas this did not work. I have the two Macro files attached as mentioned above as well as the ."Group Run "Macro. Can anyone smarter than myself at these get these running in sequence ? 

 

As a note, I usually find that the when running the Group Run Macro, the Save As Macro will fire up and do its job, but after that sequence is finished, the PDF &DWG Export Macro does not start. 

 

Furthermore, the "Group Run" Macro assumes you place the "Macros" folder in the attachment in the root of your C:/ drive to function.

 

Thanks,

E

Macros.zip

Link to comment
Share on other sites

Edit. 

 

Just like that after sleeping on it I have sorted it into one functional Macro. So if anyone is looking for a Macro that will save a 2D drawing and export said drawing into PDF,DWG or DXF then please see attached for your use 🙂

 

 

 

SAVE DRAWING WITH PDF & DWG EXPORT.zip

Link to comment
Share on other sites

I am back. 

 

So while I solved my initial problem as above. I wanted to try and make this Macro as automated as possible so that I can have the Macro initialize when a user uses the default save button within Solid works.  I found a great resource for doing this as per

WWW.CODESTACK.NET

Macro runs VBA code (or another macro) automatically on file save using SOLIDWORKS API

 

However it is clear my novice knowledge of VBA is holding me back as whenever I try to implement my current Macro code using this resource, nothing happens when I run the Macro, not even any particular errors.  Though it is clear I have done something wrong.  

 

My current Macro code is

 

............................................................................................................................

'Export Open Drawings as PDF_DWG (SW2019).swp ------------- 01/30/19

'Description: Macro to export all open drawings as PDF and DWG.

'Pre-Condition: An open drawing which has been saved and has one model view. The model should have the
' PartNumber, Revision and Description custom properties with values added.

'Post-Condition: Macro save active drawing as PDF and DXF in same location as the drawing file with
' PartNumber, Revision and Description custom properties values from referenced model as the file name.

' The macro would process all open drawings and would export only those drawings which has been saved and contains minimum one model view.

' Please back up your data before use and USE AT OWN RISK
'
'------------------------------------------------------------------------------------
' Created by Deepak Gupta (Boxer's SOLIDWORKS Blog, India) http://gupta9665.com/
'------------------------------------------------------------------------------------
' Disclaimer:
' This macro is provided as is. No claims, support, refund, safety net, or
' warranties are expressed or implied. By using this macro and/or its code in
' any way whatsoever, the user and any entities which the user represents,
' agree to hold the authors free of any and all liability.
' Free distribution and use of this code in other free works is welcome.
' You may redistribute it and/or modify it on the condition that this header is retained.
' All other forms of distribution (i.e., not free, fee for delivery, etc) are prohibited
' without the expressed written consent by the authors.
' Use at your own risk!
' ------------------------------------------------------------------------------

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDrawModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim nErrors As Long
Dim nWarnings As Long
Dim Revision As String
Dim Description As String
Dim PartNumber As String
Dim nFileName As String
Dim sFileName As String
Dim nResponse As Integer
Dim FileSave As Boolean
Dim sDrawingCol As New Collection

Sub Main()

Set swApp = Application.SldWorks
Set swDrawModel = swApp.GetFirstDocument
Set swModel = swApp.ActiveDoc

swModel.Extension.RunCommand swCommands_SaveAs, Empty
 
' Check to see if a drawing is loaded.
If swDrawModel Is Nothing Then
MsgBox "There is no active drawing document"
Exit Sub
End If

Do While Not swDrawModel Is Nothing
If swDrawModel.GetType = swDocDRAWING Then
sDrawingCol.Add swDrawModel.GetPathName
Debug.Print swDrawModel.GetPathName
End If
Set swDrawModel = swDrawModel.GetNext
Loop

If sDrawingCol.Count > 0 Then

'Set file export type
nResponse = MsgBox("Select YES (PDF & DWG) OR NO (DXF) OR CANCEL (Exit/End)?", vbYesNoCancel)
If nResponse = vbYes Then
FileSave = True
ElseIf nResponse = vbNo Then
FileSave = False
ElseIf nResponse = vbCancel Then
Exit Sub
End If
Else

MsgBox "There is no active drawing document"
Exit Sub
 
End If

Set swDrawModel = swApp.GetFirstDocument
Do While Not swDrawModel Is Nothing
If swDrawModel.GetType = swDocDRAWING Then
Set swDraw = swDrawModel
 
If swDraw.GetPathName <> "" Then
sFileName = Mid(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\") + 1)
swApp.ActivateDoc3 sFileName, False, swDontRebuildActiveDoc, nErrors
Else
 
MsgBox "This drawing: " & UCase(swDraw.GetTitle) & " has not been saved, " & vbCrLf & _
"jumping to next drawing (if available) else ending the macro"
GoTo Jump
End If
 
Set swView = swDraw.GetFirstView
Set swView = swView.GetNextView

If Not swView Is Nothing Then
Set swModel = swView.ReferencedDocument
Else
MsgBox "No model view found in this drawing: " & UCase(swDraw.GetTitle) & ", " & vbCrLf & _
"jumping to next drawing (if available) else ending the macro"
GoTo Jump
End If


'Get Model Properties
PartNumber = swModel.GetCustomInfoValue("", "Part Number")
Revision = swModel.GetCustomInfoValue("", "Revision")
Description = swModel.GetCustomInfoValue("", "Description")
 
If Revision = "" Then
Revision = ""
End If

'nFileName = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\")) & PartNumber & "-" & Revision & " " & Description
 
' Comment out above line and remove comment from following line in case you want to simply save the draing file with its own name.
nFileName = Left(swDraw.GetPathName, InStrRev(swDraw.GetPathName, "\")) & sFileName
 
If FileSave = True Then
'Save as DWG (replace DWG with DXF in following line in case you want to save as DXF)
'Note that DWG will export to one or multiple sheets depending on your solidworks DWG export settings
swDraw.SaveAs3 nFileName & ".DWG", 0, 0
'Save as PDF`
swDraw.SaveAs3 nFileName & ".PDF", 0, 0
ElseIf FileSave = False Then
'Save as DWG (replace DWG with DXF in following line in case you want to save as DXF)
swDraw.SaveAs3 nFileName & ".DXF", 0, 0
End If
End If


Jump:
Set swDrawModel = swDrawModel.GetNext
Loop
 
Set sDrawingCol = Nothing

End Sub

Anyone have any ideas ?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

This Website may place and access certain Cookies on your computer. ExtremeHW uses Cookies to improve your experience of using the Website and to improve our range of products and services. ExtremeHW has carefully chosen these Cookies and has taken steps to ensure that your privacy is protected and respected at all times. All Cookies used by this Website are used in accordance with current UK and EU Cookie Law. For more information please see our Privacy Policy