Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

SW VBA 1

Status
Not open for further replies.

33456

Mechanical
May 23, 2007
10
How can i delete my 'sheet format' with VBA ?!?
 
Replies continue below

Recommended for you

I imagine you can use VBA to make sure you are in Sheet Format mode, then have a routine that finds all entities on the sheet format and then a routine that deletes all of those entities.

Matt
CAD Engineer/ECN Analyst
Silicon Valley, CA
sw.fcsuper.com
Co-moderator of Solidworks Yahoo! Group
 
Thnx for reply ...

i have a vba routine for it, but something strange happens ..
When i manually throw away the sheet format and then undo it ... after that it works ... when i just run the macro . it doesnt ..

Here's some code:
sub justdoit()
Dim MswApp As Object
Dim MPart As Object
Dim MSelMgr As Object
Dim Mboolstatus As Boolean
Dim Mlongstatus As Long, Mlongwarnings As Long
Dim MFeature As Object
Dim Sheet As Object

Set MswApp = Application.SldWorks
Set MPart = MswApp.ActiveDoc
Set Sheet = MPart.GetCurrentSheet
shtnaam = Sheet.GetSheetFormatName
Set MSelMgr = MPart.SelectionManager
Mboolstatus = MPart.Extension.SelectByID2(shtnaam, "SHEET", 0, 0, 0, False, 0, Nothing, 0)
MPart.EditDelete
end sub
 
The problem is with your selection process. All you have is a macro-recorded process for selecting and deleting a format. Macro-recorded selections rarely work well.

Look into these commands to get the active sheet and format:[ul]
[li]DrawingDoc::GetCurrentSheet[/li]
[li]Sheet::SheetFormatVisible[/li]
[li]Sheet::GetName[/li]
[li]Sheet::GetProperties[/li]
[li]Sheet::GetSheetFormatName[/li]
[li]Sheet::SetSheetFormatName[/li]
[/ul]



[bat]Honesty may be the best policy, but insanity is a better defense.[bat]
-SolidWorks API VB programming help
 
Perhaps ... i turned it off first, but on the dxf it was still visible ...
 
You could make the macro reload the sheet with the sheet format hidden. I don't know which processes would do this in API though.

Matt
CAD Engineer/ECN Analyst
Silicon Valley, CA
sw.fcsuper.com
Co-moderator of Solidworks Yahoo! Group
 
I tried that with a forcerebuild

didnt work

too bad didnt get the correct answer yet :(
 
Here you go.
Ken

Code:
'DeleteSheetFormat

Option Explicit

Sub main()
    Dim swApp                   As SldWorks.SldWorks
    Dim swModel                 As SldWorks.ModelDoc2
    Dim swDraw                  As SldWorks.DrawingDoc
    Dim swSheet                 As SldWorks.Sheet
    Dim swSelMgr                As SldWorks.SelectionMgr
    Dim swModelDocExt           As SldWorks.ModelDocExtension
    Dim bStatus                 As Boolean
    Dim nDeleteOption           As Long
    Dim nStatus                 As Long
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    If (swModel Is Nothing) Then
        MsgBox "Must have a Drawing document open." & Chr(13) & "Routine Ending.", vbExclamation, "DeleteSheetFormat"
        End
    End If
    
    If (swModel.GetType <> swDocDRAWING) Then
        MsgBox "Must have a Drawing document open." & Chr(13) & "Routine Ending.", vbExclamation, "DeleteSheetFormat"
        End
    End If
    
    Set swModelDocExt = swModel.Extension
    Set swDraw = swModel
    Set swSheet = swDraw.GetCurrentSheet

    swModel.ClearSelection2 True
    
    Set swSelMgr = swDraw.SelectionManager
    bStatus = swModelDocExt.SelectByID2(swSheet.GetSheetFormatName, "SHEET", 0, 0, 0, False, 0, Nothing, 0)
    If bStatus Then
        
        ' To delete absorbed features, use enum SwConst.swDelete_Absorbed
        ' To delete children features, use enum SwConst.swDelete_Children
        ' To keep absorbed features and children features, set DeleteOption = 0
        'nDeleteOption = SwConst.swDelete_Absorbed
        'nDeleteOption = SwConst.swDelete_Children
        'nDeleteOption = 0
        nDeleteOption = SwConst.swDelete_Absorbed + SwConst.swDelete_Children
        
        nStatus = swModelDocExt.DeleteSelection2(nDeleteOption)
        If (nStatus = 1) Then
            swModel.ClearSelection2 True
            swModel.ForceRebuild3 (False)
        Else
            MsgBox "Delete Sheet Format failed." & Chr(13) & "*Delete* function failed.", vbExclamation, "DeleteSheetFormat"
        End If
    Else
        MsgBox "Delete Sheet Format failed." & Chr(13) & "Unable to *select* Sheet Format", vbExclamation, "DeleteSheetFormat"
    End If

End Sub
 
33456 ... just in case you are unaware of this feature; clicking the link awards a star to the poster. The stars are counted toward the MVP listing for the forum.

042eca74-0443-433d-84d5-9d6a58b2d897_6ae099ac-fff1-49e6-b88b-8b501190a7fa_static_0_0_image.png


[cheers]
 
KenBolen,

Cool code. Do you think I can post it on my resource site (in my signature)?

Also, one thing to look at for future versions (if forecoming) is that the code currently ignores inserted objects (such as images of company logos) on the sheet format.

Matt
CAD Engineer/ECN Analyst
Silicon Valley, CA
sw.fcsuper.com
Co-moderator of Solidworks Yahoo! Group
 
fcsuper,

Whatever you want to do with the code is fine with me.

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor