Hello everyone.
I am trying to program a Macro in which I can choose which sketches I want to see. I have found some similar in the API Help but this macro doesn't work like I would. This is the code.
Public Sub ToggleSketchVisibility() ' Set a reference to the Sketches collection. This assumes ' that a part document containing a sketch is active. Dim oSketches As PlanarSketches Set 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 End Sub
I need something similar but where I can choose the sketch.
Thank you.
Solved! Go to Solution.
Solved by pabloiglesiasc. Go to Solution.
Try this one working goood
'Attribute VB_Name = "Hide_Sketches_And_planes"
Sub Hide_Sketches_And_planes()
'catch and skip errors
On Error Resume Next
' Get the active document.
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument
If oDoc.DocumentType = kAssemblyDocumentObject Then
Call HideAssemblyComponents
ElseIf oDoc.DocumentType = kPartDocumentObject Then
Call HidePartComponents
Else
MsgBox ("The active file is no assembly or part. Script can't be used here.")
Exit Sub
End If
End Sub
Private Sub HideAssemblyComponents()
Dim oAsmDoc As AssemblyDocument
Set oAsmDoc = ThisApplication.ActiveDocument
'get user input as True or False
Dim Vis As Boolean
Dim result
result = MsgBox("Hide all sketches and workplanes?" & vbCrLf & vbCrLf & "Click Yes to hide all sketches and workplanes." & vbCrLf & "Click No to Show all sketches and worplanes.", vbYesNoCancel)
Select Case result
Case vbYes
Vis = False
Case vbNo
Vis = True
Case vbCancel
Exit Sub
End Select
'catch and skip errors
On Error Resume Next
Dim oSetch As PlanarSketch
Dim oWorkPlane As WorkPlane
Dim oDoc As Inventor.Document
'Iterate tru all referenced documents and change visibility
For Each oDoc In oAsmDoc.AllReferencedDocuments
For Each oWorkPlane In oDoc.ComponentDefinition.WorkPlanes
oWorkPlane.Visible = Vis
Next
'set work axis visibility
For Each oWorkAxis In oDoc.ComponentDefinition.WorkAxes
oWorkAxis.Visible = Vis
Next
'set work point visibility
For Each oWorkPoint In oDoc.ComponentDefinition.WorkPoints
oWorkPoint.Visible = Vis
Next
For Each oSketch In oDoc.ComponentDefinition.Sketches
oSketch.Visible = Vis
Next
Next
'change visibility from active assembly document
For Each oWorkPlane In oAsmDoc.ComponentDefinition.WorkPlanes
oWorkPlane.Visible = Vis
Next
'set work axis visibility
For Each oWorkAxis In oAsmDoc.ComponentDefinition.WorkAxes
oWorkAxis.Visible = Vis
Next
'set work point visibility
For Each oWorkPoint In oAsmDoc.ComponentDefinition.WorkPoints
oWorkPoint.Visible = Vis
Next
For Each oSketch In oAsmDoc.ComponentDefinition.Sketches
oSketch.Visible = Vis
Next
ThisApplication.ActiveDocument.Update
End Sub
Private Sub HidePartComponents()
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.ActiveDocument
'get user input as True or False
Dim Vis As Boolean
Dim result
result = MsgBox("Hide all sketches and workplanes?" & vbCrLf & vbCrLf & "Click Yes to hide all sketches and workplanes." & vbCrLf & "Click No to Show all sketches and worplanes.", vbYesNoCancel)
Select Case result
Case vbYes
Vis = False
Case vbNo
Vis = True
Case vbCancel
Exit Sub
End Select
Dim oSetch As PlanarSketch
Dim oWorkPlane As WorkPlane
Dim oDoc As Inventor.Document
'change visibility from active part document
For Each oWorkPlane In oPartDoc.ComponentDefinition.WorkPlanes
oWorkPlane.Visible = Vis
Next
'set work axis visibility
For Each oWorkAxis In oPartDoc.ComponentDefinition.WorkAxes
oWorkAxis.Visible = Vis
Next
'set work point visibility
For Each oWorkPoint In oPartDoc.ComponentDefinition.WorkPoints
oWorkPoint.Visible = Vis
Next
For Each oSketch In oPartDoc.ComponentDefinition.Sketches
oSketch.Visible = Vis
Next
ThisApplication.ActiveDocument.Update
End Sub
Hi,
Thank you I will try later but I have found another way. What do you think?
Dim Sketch1, Sketch2, Sketch3 As PlanarSketch Dim sketches As PlanarSketches Set sketches = ThisApplication.ActiveDocument.ComponentDefinition.sketches Dim s1, s2, s3 As PlanarSketch For Each s1 In sketches If s1.Name = "Sketch1" Then Set Sketch1 = s1 Exit For End If Next If CheckBox1.Value = 0 Then s1.Visible = False Else s1.Visible = True End If For Each s2 In sketches If s2.Name = "Sketch2" Then Set Sketch2 = s2 Exit For End If Next If CheckBox2.Value = 0 Then s2.Visible = False Else s2.Visible = True End If For Each s3 In sketches If s3.Name = "Sketch3" Then Set Sketch3 = s3 Exit For End If Next If CheckBox3.Value = 0 Then s3.Visible = False Else s3.Visible = True End If