Create Part in SubAssembly

Create Part in SubAssembly

florian_wenzel
Advocate Advocate
497 Views
5 Replies
Message 1 of 6

Create Part in SubAssembly

florian_wenzel
Advocate
Advocate

Hi,

 

Inventor 2022

API VB.NET VisualStudio

 

I try to Create a Part in SubAssembly.

Then, i want to Projecting a Curve from Sketch from Other Part.

 

I Got Error with AddByProjectingEntity

I got 2 PlanrSketchProxy, but they are in Other Assembly, is this ok.

What i need ?

 

This is my  Code:

 

 

 

Imports System.Runtime.InteropServices
Imports Inventor
Imports Microsoft.Win32

Module CommandFunctionButton_01

    Public Sub CommandFunctionfweButton_01()

        Dim oAsmDoc As AssemblyDocument = g_inventorApplication.ActiveDocument
        Dim oAsmCompDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
        Dim oTO As TransientObjects = g_inventorApplication.TransientObjects
        Dim oTG As TransientGeometry = g_inventorApplication.TransientGeometry

        'Create Matrix
        Dim oMatrix As Matrix
        oMatrix = oTG.CreateMatrix

        'Create occurence, PartComp.def, Sketch and Proxy for this Sketch
        Dim oOccPartA As ComponentOccurrence = oAsmCompDef.Occurrences.ItemByName("Part_A:1")
        Dim oPartA_Def As PartComponentDefinition = oOccPartA.Definition
        Dim oSketchPartA As PlanarSketch = oPartA_Def.Sketches("Sketch_A")
        Dim oSketchPartA_Proxy As PlanarSketchProxy
        Call oOccPartA.CreateGeometryProxy(oSketchPartA, oSketchPartA_Proxy)


        'Create New Assembly
        Dim oNameFile As String = InputBox("text")
        Dim oNewAssembly_01 As AssemblyDocument = g_inventorApplication.Documents.Add(DocumentTypeEnum.kAssemblyDocumentObject)
        oNewAssembly_01.SaveAs("C:\InventorAPI\1780-006\CAD\3D\Assembly_" & oNameFile & ".iam", False)

        'Create Assembly occurence 
        Dim oSubAssyOcc As ComponentOccurrence
        oSubAssyOcc = oAsmCompDef.Occurrences.AddByComponentDefinition(oNewAssembly_01.ComponentDefinition, oMatrix)
        oNewAssembly_01.Close()

        oSubAssyOcc.Edit()

        'Create New  Assembly Comp.def
        Dim oAsmDoc02 As AssemblyDocument
        oAsmDoc02 = g_inventorApplication.ActiveEditDocument
        Dim oAsmCompDef02 As AssemblyComponentDefinition
        oAsmCompDef02 = oAsmDoc02.ComponentDefinition


        'Create New  Part in New Assembly using New Comp.def
        Dim oNameFile2 As String = InputBox("text")
        Dim oPartB As PartDocument = g_inventorApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject)
        oPartB.SaveAs("C:\InventorAPI\1780-006\CAD\3D\SheetMetal_Curved_Inside_" & oNameFile2 & ".ipt", False)

        Dim oOccPartB As ComponentOccurrence
        oOccPartB = oAsmCompDef02.Occurrences.AddByComponentDefinition(oPartB.ComponentDefinition, oMatrix)
        oPartB.Close()


        'Create New  PartComp.def, Sketch and Proxy for this Sketch
        Dim oPartB_Def As PartComponentDefinition = oOccPartB.Definition

        Dim oSketchPartB As PlanarSketch = oPartB_Def.Sketches.Add(oPartB_Def.WorkPlanes.Item(3), False)

        Dim oSketchPartB_Proxy As PlanarSketchProxy
        Call oOccPartB.CreateGeometryProxy(oSketchPartB, oSketchPartB_Proxy)

        'Select Curve from Part A
        Dim oCurve As Object = g_inventorApplication.CommandManager.Pick(SelectionFilterEnum.kSketchCurveFilter, "Pick a Curve")


        'Projecting Curve from Proxy Sketch_A to Proxy Sketch_B
        Dim oEnt As SketchEntity
        oEnt = oSketchPartB_Proxy.AddByProjectingEntity(oCurve)

        oAsmDoc.Update()

    End Sub
End Module

 

 

 

Error:

Unknow Error HRESULT: 0x80004005 (E_FAIL))"

 

florian_wenzel_0-1654727136538.png

 

 

Assembly:

florian_wenzel_0-1654726474146.png

 

When during Creating new Part i choose the AssemblyComponentDef. from Main Assembly, then The New part is not in SubAssembly, but the Projecting is Working good.

 

Also, when i Delete, the:

florian_wenzel_3-1654726793848.png

Then the part is not add to Subassembly, but the Projecting is Working Good.

 

 

 

 

florian_wenzel_2-1654726770535.png

 

 

My Goal is to Add the Part to New SubAssembly and Projecting the Curve

 

Do i need any Subassembly Method or ComponentOccurrenceProxy etc... ?

Thanks for Any Suggestion

 

0 Likes
Accepted solutions (1)
498 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor

Hi @florian_wenzel.  Without having all the files in front of me to test with, reading down through your post kind of makes my head hurt, trying to wrap my head around all the different 'context model spaces involved with this' and proxy levels involved.  Let me first state what my understanding of your goal here is, so that you know if I'm on the right track.  I think you want to directly project sketch geometry from the sketch within the top level component part named "Part_A:1", down into a new sketch, which was created within a newly created part, which was created within a newly created sub assembly, under the main assembly.  Here's what I'm thinking...  After you create the new sketch within the new lower level part, use the AddByProjectingEntity method of that sketch directly, not from its proxy, which would exist at the sub-assembly level.  But then the sketch entity you 'Pick' for projecting to that new sketch needs to be the proxy (in the context of the main assembly) of the original part's sketch entity.  That way it will be in proper position & orientation (vertical lineage).  You may have to have the component for PartB in edit mode though to make this happen properly, and I'm not sure it is possible be in 2 levels deep of edit modes from the main assembly and make this work by code.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 6

WCrihfield
Mentor
Mentor

Maybe save, then exit out of edit mode of the sub assembly after you have added the new part's component to it. Then enter edit mode of the new part's component directly after that, before doing the sketch projection part, if it will let you do that.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 6

florian_wenzel
Advocate
Advocate

Hi,

first, thanks for Answer.

I put the Files now.

 

I was trying with those Suggestion what you wrote me.

Still Error.

I mean that i using now the directly Sketch, but without any Positve Result.

florian_wenzel_0-1654780091151.png

 

Actually I didn't understand your 2 answer:


@WCrihfield  schrieb:

Maybe save, then exit out of edit mode of the sub assembly after you have added the new part's component to it. Then enter edit mode of the new part's component directly after that, before doing the sketch projection part, if it will let you do that.


not sure what i need to do.

But 🙂

Maybe it was this.

 

I mean, what if

i create the PartB in the Main Assembly, than i can with Proxy Projecting the Curve.

Save it.

Then

Delete the PartB from Main Assembly

And

Add the PartB in SubAssembly

 

is this Possible

How to Delete Part from Main Assembly?

 

When you have any Other Suggestion, I will be grateful

 

0 Likes
Message 5 of 6

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @florian_wenzel 

There are multiple ways to do this, but I'll give you an example of how to do it with just one extra line of code added to your original code. The thing is, that both proxys must be in your top level assemblys context. As you've done it, one proxy is in the context of your subassemblys document. What this example does is that it takes that proxy and creates a proxy of it through the occurrence object of the subassembly in the main assembly. I hope my explanation makes sense to you 😉

 

I just added one row to your code:

'CREATE THE PROXY THROUGH OCCURRENCES IN THE MAIN ASSEMBLY BY CREATING A PROXY OF THE PROXY IN oSubAssyOcc
Call oSubAssyOcc.CreateGeometryProxy(oSketchPartB_Proxy, oSketchPartB_Proxy)

 So the full code is now:

Public Sub CommandFunctionfweButton_01()


		Dim oAsmDoc As AssemblyDocument = g_inventorApplication.ActiveDocument
		Dim oAsmCompDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
		Dim oTO As TransientObjects = g_inventorApplication.TransientObjects
		Dim oTG As TransientGeometry = g_inventorApplication.TransientGeometry

		'Create Matrix
		Dim oMatrix As Matrix
		oMatrix = oTG.CreateMatrix

		'Create occurence, PartComp.def, Sketch and Proxy for this Sketch
		Dim oOccPartA As ComponentOccurrence = oAsmCompDef.Occurrences.ItemByName("Part_A:1")
		Dim oPartA_Def As PartComponentDefinition = oOccPartA.Definition
		Dim oSketchPartA As PlanarSketch = oPartA_Def.Sketches("Sketch_A")
		Dim oSketchPartA_Proxy As PlanarSketchProxy
		Call oOccPartA.CreateGeometryProxy(oSketchPartA, oSketchPartA_Proxy)


		'Create New Assembly
		Dim oNameFile As String = InputBox("text")
		Dim oNewAssembly_01 As AssemblyDocument = g_inventorApplication.Documents.Add(DocumentTypeEnum.kAssemblyDocumentObject)
		oNewAssembly_01.SaveAs("C:\InventorAPI\1780-006\CAD\3D\Assembly_" & oNameFile & ".iam", False)

		'Create Assembly occurence 
		Dim oSubAssyOcc As ComponentOccurrence
		oSubAssyOcc = oAsmCompDef.Occurrences.AddByComponentDefinition(oNewAssembly_01.ComponentDefinition, oMatrix)
		oNewAssembly_01.Close()

		oSubAssyOcc.Edit()

		'Create New  Assembly Comp.def
		Dim oAsmDoc02 As AssemblyDocument
		oAsmDoc02 = g_inventorApplication.ActiveEditDocument
		Dim oAsmCompDef02 As AssemblyComponentDefinition
		oAsmCompDef02 = oAsmDoc02.ComponentDefinition


		'Create New  Part in New Assembly using New Comp.def
		Dim oNameFile2 As String = InputBox("text")
		Dim oPartB As PartDocument = g_inventorApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject)
		oPartB.SaveAs("C:\InventorAPI\1780-006\CAD\3D\SheetMetal_Curved_Inside_" & oNameFile2 & ".ipt", False)

		Dim oOccPartB As ComponentOccurrence
		oOccPartB = oAsmCompDef02.Occurrences.AddByComponentDefinition(oPartB.ComponentDefinition, oMatrix)
		oPartB.Close()


		'Create New  PartComp.def, Sketch and Proxy for this Sketch
		Dim oPartB_Def As PartComponentDefinition = oOccPartB.Definition

		Dim oSketchPartB As PlanarSketch = oPartB_Def.Sketches.Add(oPartB_Def.WorkPlanes.Item(3), False)

		Dim oSketchPartB_Proxy As PlanarSketchProxy


		Call oOccPartB.CreateGeometryProxy(oSketchPartB, oSketchPartB_Proxy)

		'CREATE THE PROXY THROUGH OCCURRENCES IN THE MAIN ASSEMBLY BY CREATING A PROXY OF THE PROXY IN oSubAssyOcc
		Call oSubAssyOcc.CreateGeometryProxy(oSketchPartB_Proxy, oSketchPartB_Proxy)

		'Select Curve from Part A
		Dim oCurve As Object = g_inventorApplication.CommandManager.Pick(SelectionFilterEnum.kSketchCurveFilter, "Pick a Curve")


		'Projecting Curve from Proxy Sketch_A to Proxy Sketch_B
		Dim oEnt As SketchEntity
		oEnt = oSketchPartB_Proxy.AddByProjectingEntity(oCurve)

		oAsmDoc.Update()
	End Sub
Message 6 of 6

florian_wenzel
Advocate
Advocate

This is Amazing!

ok, i got  it

Thanks a Lot 🙂

 

 

Still a Question what for is the: ComponentOccurrenceProxy  ?

Is this way what i use not so bad aceptable, or is a better way?

 

 

0 Likes