Announcements
Autodesk Community will be read-only between April 26 and April 27 as we complete essential maintenance. We will remove this banner once completed. Thanks for your understanding

Ilogic go into edit mode-3Dsketch from top level assembly

jaconoorland45
Contributor

Ilogic go into edit mode-3Dsketch from top level assembly

jaconoorland45
Contributor
Contributor

I have an assembly.

 

In this assembly in this assembly i have a subassembly "X" which contains a Part called "Y" which contains a 3d sketch called "Base". Then I want an ilogic rule which starts to edit this 3Dsketch. So it goes into edit mode in this sketch and then the rule finishes so the user can immiately start drawing.

 

It is important that the edit is "in place" like what happens when you double click the 3D sketch from the browser. 

 

So to summarize the in place: Assembly(Alpha) --> Subassembly(X) --> Part(Y) --> 3DSketch(Base)

 

I have tried many different approaches with Ilogic but nothing works. 

 

Is it possible to do?

 

I have Inventor 2023

 

Thanks in advance!

0 Likes
Reply
Accepted solutions (1)
206 Views
6 Replies
Replies (6)

WCrihfield
Mentor
Mentor

Have you tried something like this yet?  It uses 'iLogic' to get the current assembly, then uses Inventor API to do the rest.

Sub Main
	Dim oADoc As AssemblyDocument = TryCast(ThisDoc.Document, Inventor.AssemblyDocument)
	If oADoc Is Nothing Then Return
	Dim oSubAsmOcc As ComponentOccurrence = oADoc.ComponentDefinition.Occurrences.ItemByName("X")
	oSubAsmOcc.Edit()
	Dim oSubAsmPrt As ComponentOccurrence = oSubAsmOcc.Definition.Occurrences.ItemByName("Y")
	oSubAsmPrt.Edit()
	Dim oSketch3D As Sketch3D = oSubAsmPrt.Definition.Sketches3D.Item("Base")
	oSketch3D.Edit()
	'do stuff with 3D Sketch now
	
	'optionally exit all edit modes again
	'exit edit mode of 3D sketch
	'oSketch3D.ExitEdit()
	'exit edit mode of part
	'oSubAsmPrt.ExitEdit(ExitTypeEnum.kExitToParent)
	'exit edit mode of sub assembly
	'oSubAsmOcc.ExitEdit(ExitTypeEnum.kExitToParent)
	'oADoc.Update2(True)
	'oADoc.Save2(True)
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

jaconoorland45
Contributor
Contributor

Okay thank you. so what doesnt work is oSubAsmPrt.Edit() in line 7. Does it work for you? the error i get is this: 

jaconoorland45_0-1744363064403.png

 

0 Likes

jaconoorland45
Contributor
Contributor

So I think the code doesn't work because it is ran from a top level assembly. what can you do in your code so that it always works? A screenshot below which illustrates what i want to do with the code. it should be super simple yet i cannot come up with anything which accomplishes this without throwing an error. 

jaconoorland45_0-1744367612942.png

 

0 Likes

WCrihfield
Mentor
Mentor
Accepted solution

Sorry for the coding mistake on my part, and for the long delay on getting back here.  Too many things going on at the same time.

So, in this case we would not enter into 'edit mode' of the sub assembly occurrence.  And, we would not access the 'part' occurrence within that sub assembly through the 'definition' of the sub assembly, but rather through the sub assembly occurrences' sub components.  What I mean is the ComponentOccurrence object representing the sub assembly itself has a property named SubOccurrences.  That property returns a ComponentOccurrencesEnumerator type collection object, which is different than the ComponentOccurrences collection object that we get from a ComponentDefinition's Occurrences property.  Even though the occurrence representing the part within the sub assembly can be reached either way, there is an important difference between which route you take.  In this case, since we want to be 'in-place editing' that sub-sub-component, within the context of a higher level assembly, we will need to use the SubOccurrences route, to maintain that 'lineage' or association with the 'Parent' assembly.  But when using the ComponentOccurrencesEnumerator, it only has the 'Item' property for accessing its contents, which only allows for 'Integer/Long' type input for specifying which one you want.  So, in order to get a specific component within that collection by its name, we will have to iterate through all of its items, checking the name of each one against the name we are looking for, then capture the one with the matching name, if it can be found.

Once the occurrence representing the part is found, we can then enter into its 'edit mode'.  This will essentially bypass its parent sub assembly's occurrence, letting you edit that sub assembly's part directly from within the context of the higher level assembly.  Once we are in edit mode of that occurrence, we can then either find an existing 3D sketch by the specified name, or try to start a new 3D sketch, then assign that name to it.  Then, once it has either been found, or created, we can enter into 'edit mode' of that 3D sketch.

Below is a 'working' example code that I modified from my earlier attempt, and tested it on a local assembly, but with different component names.

Sub Main
	Dim oADoc As AssemblyDocument = TryCast(ThisDoc.Document, Inventor.AssemblyDocument)
	If oADoc Is Nothing Then Return
	
	'<<< CHANGE THESE NAMES AS NEEDED - MUST BE EXACTLY LIKE MODEL BROWSER TREE NAMES >>>
	'
	Dim sSubAsssemblyOccurrenceName As String = "SubAsm1:1"
	Dim sPartOccurrenceName As String = "Part1:1"
	Dim s3DSketchName As String = "Base"
	
	'<<< FIND THE SUB ASSEMBLY OCCURRENCE >>>
	Dim oSubAsmOcc As ComponentOccurrence = Nothing
	For Each oOcc In oADoc.ComponentDefinition.Occurrences
		If oOcc.Name = sSubAsssemblyOccurrenceName Then
			oSubAsmOcc = oOcc
			Exit For 'exit loop, keeping variable value
		End If
	Next
	If oSubAsmOcc Is Nothing Then
		MsgBox("Sub-Assembly Not Found!", vbCritical, "Sub-Assembly Not Found")
		Return 'exit this code routine / rule
	End If
	
	'<<< FIND THE PART OCCURRENCE WITHIN THE SUB ASSEMBLY'S SUB-OCCURRENCES >>>
	Dim oSubAsmPrtOcc As ComponentOccurrence = Nothing
	For Each oSubAsmSubComp As ComponentOccurrence In oSubAsmOcc.SubOccurrences
		If oSubAsmSubComp.Name = sPartOccurrenceName Then
			oSubAsmPrtOcc = oSubAsmSubComp
			Exit For
		End If
	Next
	If oSubAsmPrtOcc Is Nothing Then
		MsgBox("Sub Assembly Part Not Found!", vbCritical, "Part Not Found")
		Return 'exit this code routine / rule
	End If
	'<<< START EDIT MODE OF PART OCCURRENCE >>>
	oSubAsmPrtOcc.Edit()
	
	'<<< FIND 3D SKETCH >>>
	Dim oSubAsmPrtOccSketches3D As Sketches3D = oSubAsmPrtOcc.Definition.Sketches3D
	Dim oBaseSketch3D As Sketch3D = Nothing
	For Each oS3D As Sketch3D In oSubAsmPrtOccSketches3D
		If oS3D.Name = s3DSketchName Then
			oBaseSketch3D = oS3D
			Exit For
		End If
	Next
	'<<< IF NOT FOUND, CREATE IT, THEN NAME IT, ELSE, START ITS EDIT MODE >>>
	If oBaseSketch3D Is Nothing Then
		oBaseSketch3D = oSubAsmPrtOccSketches3D.Add()
		oBaseSketch3D.Name = s3DSketchName
	Else
		oBaseSketch3D.Edit()
	End If
End Sub

There are likely ways to do this same process using much less code, if incorporating some of the uniquely iLogic tools, instead of purely Inventor API code, but this is the proper process for accomplishing this task, and it worked OK for me. 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

jaconoorland45
Contributor
Contributor

Thank you for the quick reply! I will test the code saturday! I will let you know.

0 Likes

jaconoorland45
Contributor
Contributor

Thank you! It works like a charm!

0 Likes

Type a product name