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
Solved! Go to Solution.
Solved by WCrihfield. Go to Solution.
Solved by WCrihfield. Go to Solution.
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
(Not an Autodesk Employee)
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. 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
(Not an Autodesk Employee)
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
(Not an Autodesk Employee)
Can't find what you're looking for? Ask the community or share your knowledge.