Control visibility of sketches in a part file

Control visibility of sketches in a part file

PappaJeff
Contributor Contributor
1,035 Views
6 Replies
Message 1 of 7

Control visibility of sketches in a part file

PappaJeff
Contributor
Contributor

I have a part file that just contains sketch that are used as references in an assembly. I want to turn off the sketches based on a variable output. When I run the program shown below, I get an error message. I am new to programing and would be grateful for any help. 

I am running Inventor 2021

 

Thank you,

 

Jeff 

 

PappaJeff_1-1711472562305.png

 

PappaJeff_0-1711472548233.png

 

0 Likes
Accepted solutions (2)
1,036 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Hi @PappaJeff. Add "ComponentDefinition" (unquoted) between "oDoc" variable and "Sketches" on Line 3, then specify what type of object the variable represents also, like this:

 

For Each oSketch As PlanarSketch In oDoc.ComponentDefinition.Sketches

 

 'or get the sketches to a variable first, then iterate through its members, like this:

 

Dim oSketches As PlanarSketches = oDoc.ComponentDefinition.Sketches
For Each oSketch As PlanarSketch In oSketches

Edit:  Actually, there are more problems that just that within your code above.  It would be easier to copy, edit, then post back here again, if you copied your real code text, then pasted that text here in a response.  But if/when you do, use the </> symbol within the forum response tools to post your code within a code window, like in this response.  It is easier to look at and work with that way.

One of the things that should change is changing "Else If" to "ElseIf" (no space between Else and If).  Another thing is that the same single Sketch mentioned at the start of the 'loop' should not be renamed that many times, because that's what it looks like it would by trying to do.  You would need a bunch of If...Then statements added in there for each sketch name you want to check for.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 7

PappaJeff
Contributor
Contributor

Hello Wesley, 

Thank you for your response. I implemented the second option and got a different "unspecified" error.   

Any ideas to the cause?

 

Jeff

 

PappaJeff_1-1711476097875.png

 

PappaJeff_2-1711476124543.png

 

 

PappaJeff_0-1711476076449.png

 

0 Likes
Message 4 of 7

WCrihfield
Mentor
Mentor
Accepted solution

@PappaJeff.  The error is happening when you try to set a new name to a sketch.  This is likely because your code is currently trying to rename the same sketch 4 times.  For example, if 'COURSE_NUMBER' = 1, then it will set the name of oSketch object to one name, then set its visibility, then change its name to another name, then set its visibility again, and so on, 4 times in a row.  This is because you are not dividing those up into a second level (or nested) If...ElseIf...End If type statement.  After you check the value of COURSE_NUMBER, you then need to start an internal If statement with 4 sections...one for each possible sketch name.  If name = first name, then set visibility...ElseIf name = second name, then set visibility, and so on.  It probably can not change the name of the same sketch that many times in a row super quick like what it was trying to do.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 7

PappaJeff
Contributor
Contributor

Wesley,

This what I came up with and it works. 

Thank you for the help.

 

Jeff

PappaJeff_0-1711479062367.png

 

0 Likes
Message 6 of 7

WCrihfield
Mentor
Mentor
Accepted solution

Hi @PappaJeff.  I was thinking of changing it more like the example below, but I was hoping to not have to type the whole think out like this.  I was honestly hoping you would select your actual code text (not just a picture of it, but the actual text) and paste it here within a code window, like the one below, using the </> tool of the forum response, though.  😅

Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument
Dim oSketches As PlanarSketches = oDoc.ComponentDefinition.Sketches
For Each oSketch As PlanarSketch In oSketches
	If COURSE_NUMBER = 1 Then
		If oSketch.Name = "DOME INSIDE PLATE SKETCH" Then
			oSketch.Visible = True
		ElseIf oSketch.Name = "DOME SECONDARY PLATE SKETCH" Then
			oSketch.Visible = False
		ElseIf oSketch.Name = "DOME TERTIARY PLATE SKETCH" Then
			oSketch.Visible = False
		ElseIf oSketch.Name = "DOME QUAD PLATE SKETCH" Then
			oSketch.Visible = False
		End If
	ElseIf COURSE_NUMBER = 2 Then
		If oSketch.Name = "DOME INSIDE PLATE SKETCH" Then
			oSketch.Visible = True
		ElseIf oSketch.Name = "DOME SECONDARY PLATE SKETCH" Then
			oSketch.Visible = True
		ElseIf oSketch.Name = "DOME TERTIARY PLATE SKETCH" Then
			oSketch.Visible = False
		ElseIf oSketch.Name = "DOME QUAD PLATE SKETCH" Then
			oSketch.Visible = False
		End If
	ElseIf COURSE_NUMBER = 3 Then
		If oSketch.Name = "DOME INSIDE PLATE SKETCH" Then
			oSketch.Visible = True
		ElseIf oSketch.Name = "DOME SECONDARY PLATE SKETCH" Then
			oSketch.Visible = True
		ElseIf oSketch.Name = "DOME TERTIARY PLATE SKETCH" Then
			oSketch.Visible = True
		ElseIf oSketch.Name = "DOME QUAD PLATE SKETCH" Then
			oSketch.Visible = False
		End If
	ElseIf COURSE_NUMBER = 4 Then
		If oSketch.Name = "DOME INSIDE PLATE SKETCH" Then
			oSketch.Visible = True
		ElseIf oSketch.Name = "DOME SECONDARY PLATE SKETCH" Then
			oSketch.Visible = True
		ElseIf oSketch.Name = "DOME TERTIARY PLATE SKETCH" Then
			oSketch.Visible = True
		ElseIf oSketch.Name = "DOME QUAD PLATE SKETCH" Then
			oSketch.Visible = True
		End If
	End If
Next 'oSketch
RuleParametersOutput
oDoc.Update2(True)

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 7

PappaJeff
Contributor
Contributor

Thank you, kind, sir. 😒

Hate learning the hard way but it seems to sink in when I do. 

Much appreciated

 

Jeff

0 Likes