Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Macro to open, forcerebuild, close sw docs

Status
Not open for further replies.

fcsuper

Mechanical
Apr 20, 2006
2,204
0
0
US
Replies continue below

Recommended for you

Enjoy.

Code:
Option Explicit
 
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim strPathAndFilename As String
Dim strResponse As String
Dim strFileType As Long
Dim longstatus As Long
Dim longwarnings As Long

Sub main()
    
    Set swApp = Application.SldWorks
    
    strPathAndFilename = "C:\Temp\Part1.SLDDRW"
    strResponse = vbYes
    
    If StrComp((UCase$(Right$(strPathAndFilename, 7))), ".SLDPRT", vbTextCompare) = 0 Then
        strFileType = swDocPART
    ElseIf StrComp((UCase$(Right$(strPathAndFilename, 7))), ".SLDASM", vbTextCompare) = 0 Then
        strFileType = swDocASSEMBLY
    ElseIf StrComp((UCase$(Right$(strPathAndFilename, 7))), ".SLDDRW", vbTextCompare) = 0 Then
        strFileType = swDocDRAWING
    End If
    
    Set swModel = swApp.OpenDoc6(strPathAndFilename, strFileType, 0, "", longstatus, longwarnings)
    Set swModel = swApp.ActivateDoc2(strPathAndFilename, False, longstatus)
        
    If (swModel Is Nothing) Then
        strResponse = MsgBox("The file could not be found." & Chr(13) & "Routine Ending.", vbCritical, "FileOpenRebuildSaveClose")
        End
    End If
    
    If (swModel.IsOpenedReadOnly = "False") Then
        
        If (swModel.GetType <> swDocDRAWING) Then
            
            'Shade Part
            swModel.ViewDisplayShaded
            
            'Set view
            'swModel.ShowNamedView2 "*Isometric", 7
            'swModel.ShowNamedView2 "*Trimetric", 8
            swModel.ShowNamedView2 "*Dimetric", 9
            
            'Set Feature Manager Splitter Position
            swModel.FeatureManagerSplitterPosition = 0.3
        
        End If
        
        'Rebuild File
        'swModel.EditRebuild3 'Stoplight or [Ctrl]+B
        swModel.ForceRebuild '[Ctrl]+Q
        
        'Zoom to extents
        swModel.ViewZoomtofit2
        
        'Save
        swModel.Save2 False
    
    Else
        strResponse = MsgBox("The file is Read-Only." & Chr(13) & "Do you want to close the file without Saving?", vbCritical + vbYesNo, "FileOpenRebuildSaveClose")
    End If
    
    Set swModel = Nothing
    
    If (strResponse = vbYes) Then
        'Close
        swApp.CloseDoc strPathAndFilename
    End If
    
    Set swApp = Nothing
    
End Sub
 
I have mixed and matched code I wrote and code I've found that I couldn't really tell you where everything in here came from. I guess I am the author, but I have gotten bits and pieces of the code from different places over the years (just like most everything else I've written).

Ken
 
Hey I subscribed here now, I was the one who asked on comp.cad.sw

Thanks for helping me out, I will work with this, need it to run a whole dir drawing by drawing. Will see what I can come up with myself and then get back if I wont succeed.
 
fcsuper,

if you're interested, I've written an outside SolidWorks macro .

It rebuild an Assembly and all parts which compose the assembly.
In the rebuilding, all configurations are activated to "forceRegen" them.

Maybe this macro interests you (it's a self executable which starts SW)

SW2007 SP4
Worksatation HP wx4300 2GB
NVidia Quadro FX 3450
 
Status
Not open for further replies.
Back
Top