Inventor General Discussion

Inventor General Discussion

Reply
Employee
fanj
Posts: 23
Registered: ‎03-05-2009
Message 11 of 14 (194 Views)

Re: Macro to turn visibility for particular Sketch

07-30-2012 10:54 PM 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 ETO Quality Assurance Team
Autodesk, Inc.
Distinguished Contributor
ADNpati
Posts: 126
Registered: ‎07-01-2012
Message 12 of 14 (183 Views)

Re: Macro to turn visibility for particular Sketch

08-02-2012 02:57 AM in reply to: fanj

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 -

-------------------------------------------------------------------------------------
Distinguished Contributor
ADNpati
Posts: 126
Registered: ‎07-01-2012
Message 13 of 14 (152 Views)

Re: Macro to turn visibility for particular Sketch

10-10-2012 06:01 PM in reply to: fanj

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 -

-------------------------------------------------------------------------------------
Distinguished Contributor
ADNpati
Posts: 126
Registered: ‎07-01-2012
Message 14 of 14 (126 Views)

Re: Macro to turn visibility for particular Sketch

10-30-2012 01:45 PM in reply to: fanj

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 -

-------------------------------------------------------------------------------------
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.