Community
Inventor Forum
Welcome to Autodesk’s Inventor Forums. Share your knowledge, ask questions, and explore popular Inventor topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Macro to turn visibility for particular Sketch

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
ADNpati
1040 Views, 13 Replies

Macro to turn visibility for particular Sketch

 

Hello,

 

As we have layers in AutoCAD Product, I Started using different sketches and using them as layers by turning the visibility on and off.

 

Can we write a macro (button Click) to turn one perticular Sketch on and off???

 

For example If I had a Sketch named as GROUNDFLOOR,  can I use a macro to turn them off and on when needed??

 

Thanks

AD 

Mechanical Engineer
Inventor Applications Engineer

--------------------------------------------------------------------------------------

If my solution seems to remedy your problem, please press the Accept Solution button, Some KUDOS -

-------------------------------------------------------------------------------------
Tags (2)
13 REPLIES 13
Message 2 of 14
Xun.Zhang
in reply to: ADNpati

Hi AD,

 

Contact with API expert Jane, it possiblly to add an VBA/VB code to turn on/off the visibility of perticular Sketch, but for button provided, need more consideration.

 

Xun


Xun
Message 3 of 14
JaneFan
in reply to: ADNpati

Hello,

 

How about runt he macro in VBA?

Sub test()
    Call SetVisible(True)
End Sub

Sub SetVisible(bFlag)
    Dim oDoc As PartDocument
    Set oDoc = ThisApplication.ActiveDocument
   
    Dim oSks As PlanarSketches
    Set oSks = oDoc.ComponentDefinition.Sketches
   
    Dim oSketch As PlanarSketch
    For Each oSketch In oSks
        If oSketch.Name = "GROUNDSketch" Or oSketch.Name = "What I Need else" Then
            oSketch.Visible = bFlag
        End If
    Next
End Sub




Jane Fan
Inventor QA Engineer
Message 4 of 14
ADNpati
in reply to: JaneFan

Hello Fanj, Thnaks for the time and reply with macro I tried with that and there is error coming up when running that. Find the attachment, I'll use the Macro from you and do corrections. Hopefully we will get a solution to dat.. Regards, AD
Mechanical Engineer
Inventor Applications Engineer

--------------------------------------------------------------------------------------

If my solution seems to remedy your problem, please press the Accept Solution button, Some KUDOS -

-------------------------------------------------------------------------------------
Message 5 of 14
Yijiang.Cai
in reply to: ADNpati

Please make sure the sketch name is what you want to turn off/on in the code line below

        If oSketch.Name = "GROUNDSketch" Or oSketch.Name = "What I Need else" Then
            oSketch.Visible = bFlag
        End If

 

And I think you could also use the "find button" on the top of inventor browser panel to get the right sketch, and turn on/off its visibility. Please see the attached image.

Thanks,
River Cai

Inventor Quality Assurance Team
Autodesk, Inc.
Email: River-Yijiang.Cai@autodesk.com
Message 6 of 14
ADNpati
in reply to: Xun.Zhang

Hi Xun.Zhang

 

Sure, i will contact and thanks for reply

 

AD

Mechanical Engineer
Inventor Applications Engineer

--------------------------------------------------------------------------------------

If my solution seems to remedy your problem, please press the Accept Solution button, Some KUDOS -

-------------------------------------------------------------------------------------
Message 7 of 14
ADNpati
in reply to: Yijiang.Cai

hello River,

 

I tried tried and tried to fix that error and it is saying as missmatch error.

Included picture if what I am doing.

If you know something abt it let me know

 

Thanks

Mechanical Engineer
Inventor Applications Engineer

--------------------------------------------------------------------------------------

If my solution seems to remedy your problem, please press the Accept Solution button, Some KUDOS -

-------------------------------------------------------------------------------------
Message 8 of 14
Yijiang.Cai
in reply to: ADNpati

Based on the error in the image, it looks that you want to use this macro in assembly document. But the macro you used is only for part document. If you want to use the macro for assembly document, we should refine the macro to accomplish it.

 

Thanks,
River Cai

Inventor Quality Assurance Team
Autodesk, Inc.
Email: River-Yijiang.Cai@autodesk.com
Message 9 of 14
JaneFan
in reply to: ADNpati

Hi AD,

 

I guess you opened an assembly document, instead of part document. to make the code supports all types of document, please change the document type to Document instead of PartDocument. Use following code instead to have a try:

Sub test()
    Call SetVisible(True)
End Sub

Sub SetVisible(bFlag)
    Dim oDoc As Document
    Set oDoc = ThisApplication.ActiveDocument
   
    Dim oSks As PlanarSketches
    Set oSks = oDoc.ComponentDefinition.Sketches
   
    Dim oSketch As PlanarSketch
    For Each oSketch In oSks
        If oSketch.Name = "GROUNDSketch" Or oSketch.Name = "What I Need else" Then
            oSketch.Visible = bFlag
        End If
    Next
End Sub




Jane Fan
Inventor QA Engineer
Message 10 of 14
JaneFan
in reply to: JaneFan

And there is another comments:

If you want to change the visibllity staus of the sketches in assembly document, they the code above works.

But it won't work on the sketches of assembly occurrences. It will work in this way if you want to change the sketch status in assembly occurrences:

 

Sub Test()
    'Input True or False
    Call ChangeVisiblityOfAssySketch(True)
End Sub
Sub ChangeVisiblityOfAssySketch(bFlag)
    Dim odoc As AssemblyDocument
    Set odoc = ThisApplication.ActiveDocument
   
    Dim i As Integer
    Dim oSk As PlanarSketch
    For i = 1 To odoc.ComponentDefinition.Occurrences.Count
        For Each oSk In odoc.ComponentDefinition.Occurrences
            If oSk.Name = "What you need" Then
                oSk.Visible = bFlag

                Exit For
            End If
        Next
    Next
End Sub




Jane Fan
Inventor QA Engineer
Message 11 of 14
JaneFan
in reply to: ADNpati

Hi AD,

 

Considering the complex condition of your modeling file that I'm afraid there may be sub assembly in your assembly document, so I write another routine for you to use recrsion to foreach every sub node, here is the code, hope it helps.

'*************************Start*********************************

Sub Test()
    Dim oDoc As Document
    Set oDoc = ThisApplication.ActiveDocument
    

    'Rename the first parameter "MyTest" to any name of the sketch you want to control
    'Input True or False to make the sketch visible or invisible

  Call GetPartOccurrence("MyTest", True, oDoc)
End Sub

Function GetPartOccurrence(strSketchName As String, bFlag As Boolean, oDoc As Document) As Document
   
    Dim i As Integer
    Dim oSubDoc As Document
    For i = 1 To oDoc.ComponentDefinition.Occurrences.Count
        If oDoc.ComponentDefinition.Occurrences(i).Definition.Type = kAssemblyComponentDefinitionObject Then
            Set oSubDoc = oDoc.ComponentDefinition.Occurrences(i).Definition.Document
            Call GetPartOccurrence(strSketchName, bFlag, oSubDoc)
        ElseIf oDoc.ComponentDefinition.Occurrences(i).Definition.Type = kPartComponentDefinitionObject Then
            For Each oSk In oDoc.ComponentDefinition.Occurrences(i).Definition.Sketches
                If oSk.Name = strSketchName Then
                    oSk.Visible = bFlag
                    Exit For
                End If
            Next
        End If
    Next
End Function

'*************************End*********************************




Jane Fan
Inventor QA Engineer
Message 12 of 14
ADNpati
in reply to: JaneFan

Hi jane,

 

Thanks for writing such a long code..

 

I'll execute today and keep you posted...

 

Last code works fine for me within assembly and also used changing it to part.. worked really good.

 

I'll post those codes to nite or in couple of day aftere some testing..

 

Regards,

AD

Mechanical Engineer
Inventor Applications Engineer

--------------------------------------------------------------------------------------

If my solution seems to remedy your problem, please press the Accept Solution button, Some KUDOS -

-------------------------------------------------------------------------------------
Message 13 of 14
ADNpati
in reply to: JaneFan

Hello fanj,

 

I tried this macro and Working Very Fine.

 

I am writing another macro to trun on and off A preticular sketch which is in sub-assembly in Main assembly.

Code is running finw without errors, but sketches are not turning on.

Please have a look to the code and feel free to suggest any changes:

 

Sub ToggleSketchVisibilityOn()

' Set a reference to the Sketches collection. This assumes
' that a part document containing a sketch is active.
Dim oAsmDoc As AssemblyDocument
Set oAsmDoc = ThisApplication.ActiveDocument
Dim oCompDef As ComponentDefinition
Set oCompDef = oAsmDoc.ComponentDefinition
Dim oOcc As ComponentOccurrences
Dim oOccDoc As Document
Dim oSketchname As String
oSketchname = "F"
Dim i As Integer
For i = 1 To oCompDef.occurrences.Count
If oAsmDoc.ComponentDefinition.occurrences(i).Definition.Type = kPartComponentDefinitionObject Then
Exit Sub
Else
If oAsmDoc.ComponentDefinition.occurrences(i).Definition.Type = kAssemblyComponentDefinitionObject Then
Set oOccDoc = oAsmDoc.ComponentDefinition.occurrences(i).Definition.Document
End If
End If
Dim oSketches As PlanarSketches
Set oSketches = ThisApplication.ActiveDocument.ComponentDefinition.Sketches
Dim oSketch As PlanarSketch

Set oSketches = oOccDoc.ComponentDefinition.Sketches

For Each oSketch In oSketches
If InStr(oSketch.name, oSketchname) Then
oSketch.Visible = True
End If
Next
Next
End Sub

 

 

Thanks

 

Mechanical Engineer
Inventor Applications Engineer

--------------------------------------------------------------------------------------

If my solution seems to remedy your problem, please press the Accept Solution button, Some KUDOS -

-------------------------------------------------------------------------------------
Message 14 of 14
ADNpati
in reply to: JaneFan

Here is Solution...

 

Private Sub Test_SubAssemblySketches()

Dim oAssyDoc As AssemblyDocument
Set oAssyDoc = ThisApplication.ActiveDocument

Dim oAssyDef As AssemblyComponentDefinition
Set oAssyDef = oAssyDoc.ComponentDefinition

'toggle visibility of sketches in the main assembly
Dim oSk As Sketch
For Each oSk In oAssyDef.Sketches

oSk.Visible = Not oSk.Visible

Next

'process all subassemblies
Dim oOcc As ComponentOccurrence
For Each oOcc In oAssyDef.occurrences
If oOcc.Definition.Type = kAssemblyComponentDefinitionObject Then
Dim oDef As AssemblyComponentDefinition
Set oDef = oOcc.Definition
'toggle visibility of sketches in subassemblies
Dim oSkProxy As PlanarSketchProxy
For Each oSk In oDef.Sketches

Call oOcc.CreateGeometryProxy(oSk, oSkProxy)
oSkProxy.Visible = Not oSkProxy.Visible

Next
End If
Next 'oOcc

End Sub

 

 

 

For Sketches in Subassemblies, Proxy should be used....

Mechanical Engineer
Inventor Applications Engineer

--------------------------------------------------------------------------------------

If my solution seems to remedy your problem, please press the Accept Solution button, Some KUDOS -

-------------------------------------------------------------------------------------

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report