Turn off Visibility of Part & Subassembly Sketches from Top Level

Turn off Visibility of Part & Subassembly Sketches from Top Level

Zackery.Breeland
Contributor Contributor
556 Views
4 Replies
Message 1 of 5

Turn off Visibility of Part & Subassembly Sketches from Top Level

Zackery.Breeland
Contributor
Contributor

I have been fighting this for 2 days now trying to make a code that will turn off all work feature visibility for every part and subassembly from a top level assembly.  I am doing this because many older model I work with will open up with everything visible and it makes the model incredibly slow and I do not like turning off the sketch Object Visility through the View tab.

 

I have gotten working code for the work planes, axes and points, but the sketches are not working for me.  I have found a simple code to make sure it works in just the part level, posted below:

 
' Set a reference to the Sketches collection.  This assumes
    ' that a part document containing a sketch is active.
    Dim oSketches As PlanarSketches
    oSketches = ThisApplication.ActiveDocument.ComponentDefinition.Sketches

    ' Get whether the sketch visibility should be turned on or off.
    Dim bVisibleOn As Boolean
    If MsgBox("Do you want to turn all sketches on?", vbYesNo + vbQuestion) = vbYes Then
        bVisibleOn = True
    Else
        bVisibleOn = False
    End If

    ' Iterate through all of the sketches and set their visibility.
    Dim oSketch As PlanarSketch
    For Each oSketch In oSketches
        If bVisibleOn Then
            oSketch.Visible = True
        Else
            oSketch.Visible = False
        End If
    Next
    
    Dim partDef As PartComponentDefinition
    partDef = ThisApplication.ActiveDocument.ComponentDefinition
    Dim o3DSketches As Sketches3D
    o3DSketches = partDef.Sketches3D
    Dim oSketch3D As Sketch3D
    For Each oSketch3D In o3DSketches
        If bVisibleOn Then
            oSketch3D.Visible = True
        Else
            oSketch3D.Visible = False
        End If
    Next

And I simplified that to make it automatically turn off 2D sketches in a part:

Dim oPart As PartDocument = ThisApplication.ActiveDocument

  Dim oSketches As PlanarSketches
    oSketches = oPart.ComponentDefinition.Sketches
    Dim oSketch As PlanarSketch
    For Each oSketch In oSketches
        oSketch.Visible = False
    Next

But when I get to the assembly level code I am struggling.  I have managed to write up a small code while testing that will purse through the parts in an assembly, but I can't get it to affect the sketches:

Dim oAssyDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oAssyDef As AssemblyComponentDefinition = oAssyDoc.ComponentDefinition
Dim oDoc As Document

For Each oDoc In oAssyDoc.AllReferencedDocuments
	oDoc = ThisApplication.ActiveDocument
  Dim oSketches As PlanarSketches
    oSketches = ThisApplication.ActiveDocument.ComponentDefinition.Sketches
    Dim oSketch As PlanarSketch
    For Each oSketch In oSketches
        oSketch.Visible = False
    Next
	'MsgBox("iterate")
Next 

 I don't understand why you have to use the Document type in order to run through every part in an assembly, but then you can't reference ComponentDefinition.Sketches unless you're looking at a PartDocument type.  This is causing conflicts in how I'm setting this up.  I also tried setting this up as a Main and sub routine where the subroutine was simply the code I posted, that works on the part level, and the Main routine just runs through the parts, but I continued to get errors when trying to run it.

0 Likes
Accepted solutions (1)
557 Views
4 Replies
Replies (4)
Message 2 of 5

A.Acheson
Mentor
Mentor

You were almost there. Just needed to target the referenced document. You don't necessarily need to declare as a part/assembly document. If you know the syntax or refer to the help documents you can just write it generically to a document. Looking at the document under Derived Classes  you can go to Assembly/Part Document and find ComponentDefinition property under each. 

AAcheson_0-1671074537945.png

 

Dim oAssyDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oAssyDef As AssemblyComponentDefinition = oAssyDoc.ComponentDefinition

For Each oDoc As Document In oAssyDoc.AllReferencedDocuments
	
	Dim oSketches As PlanarSketches = oDoc.ComponentDefinition.Sketches
	
    For Each oSketch As PlanarSketch In oSketches
        oSketch.Visible = False
    Next
Next 

oAssyDoc.Update

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 5

Zackery.Breeland
Contributor
Contributor

Thank you for the links to the code reference library, I'm going through that now.  I took that code you modified and placed it into one of my sub-assemblies I'm testing in, and it ran but did not turn any of the sketches off.

0 Likes
Message 4 of 5

A.Acheson
Mentor
Mentor
Accepted solution

You may need to open the part file it is modifying to confirm it is working. If it is working in the part document and not in the assembly this will because of the design view rep of the assembly. Here you have the decision to link the view reps ( associative) meaning it follows the part document or use the assembly view rep which is an overwrite of the document view rep. In most case people keep the part document as Master. In my testing if the part sketch is made visible in the assembly it will break the link to the master view rep of the part. If you just try and relink the master view rep ( no associative is available) this refreshes the link. Ideally you would have a separate design view rep in the part and select associative. If all that fails this could then be a proxy sketch issue in the assembly. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 5 of 5

Zackery.Breeland
Contributor
Contributor

You are absolutely right regarding the design view reps being out of sync and causing the issue.  I feel like I've burned so much time fighting this lol.  Thank you very much.

0 Likes