- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
Solved! Go to Solution.