Project geometry from one component to another

Project geometry from one component to another

keithjk
Advocate Advocate
947 Views
7 Replies
Message 1 of 8

Project geometry from one component to another

keithjk
Advocate
Advocate

I am trying to write some code that will automatically project geometry from a component occurrence inserted into an assembly drawing.  The program loops through all of the components in the assembly and looks for a sketch named "sk_Cutout".  If it finds that sketch, it then needs to project the geometry from the component sketch ("sk_Cutout") to the assembly sketch named "sketchDoor".  (A screenshot of a "sk_Cutout" sketch is attached).

 

The follow code is my start on this project.  Everything works well except for the most important part (and that is projecting the geometry).  I have noted in the code below where the program crashes.


Any ideas?

 

 

Public Sub extrudeSketch()

        'this checks to make sure Inventor is open and
        'sets oAssemblyDocument to the active inventor document
        CheckInventor()

        'set some variables
        Dim oDef As AssemblyComponentDefinition
        oDef = oAssemblyDocument.ComponentDefinition
        Dim oPartDef As ComponentDefinition
        Dim oOcc As ComponentOccurrence
        Dim oOccs As ComponentOccurrences = oDef.Occurrences
        Dim sk As Sketch = Nothing
        Dim skEnt As SketchEntity = Nothing
        Dim skEntNew As SketchLine = Nothing
        Dim oSketchLine As SketchLine = Nothing

        'sketch door is where the new sketch geometry will be placed
        Dim sketchDoor As PlanarSketch = oAssemblyDocument.ComponentDefinition.Sketches.Item("sketchDoor")

        'loop through all of the components in the assembly
        For Each oOcc In oOccs
            oPartDef = oOcc.Definition
            'loop through all of the sketches in the component
            'search for sketch name "sk_Cutout"
            For Each sk In oPartDef.Sketches
                If sk.Name = "sk_Cutout" Then
                    'we have found the sketch we are looking for
                    sketchDoor.Edit()
                    For Each skEnt In sk.SketchEntities
                        If skEnt.Type = ObjectTypeEnum.kSketchLineObject Then
                            oSketchLine = skEnt
                            Debug.Print(oSketchLine.Length.ToString)
                            Debug.Print(skEnt.Type.ToString)
                            '///////////  here is where the program crashes  ///////////////////
                            skEntNew = sketchDoor.AddByProjectingEntity(oSketchLine)
                        End If
                    Next
                    'exit the sketch
                    sketchDoor.ExitEdit()
                End If
            Next
        Next
    End Sub

0 Likes
Accepted solutions (1)
948 Views
7 Replies
Replies (7)
Message 2 of 8

adam.nagy
Autodesk Support
Autodesk Support

Hi,

 

Can you do the exact same thing through the UI?

If so, then I think the issue is with wrong context as described here:
http://adndevblog.typepad.com/manufacturing/2016/02/include-sketch-from-a-part-multi-level-deep.html

 

Cheers,



Adam Nagy
Autodesk Platform Services
0 Likes
Message 3 of 8

keithjk
Advocate
Advocate

Yes, I can easily do it through the UI.  I will take a look a the code in the link you provided and see if I can get it to work.

 

Thanks

0 Likes
Message 4 of 8

keithjk
Advocate
Advocate

Adam,


Thanks for the link and your input but I still can't get it to work.  Maybe if I explained how I do it throught UI, then we could figure out what is wrong with my code. 

 

Through the UI I do the following (Since the part is already placed in the assembly, and constrained properly, I won't mention those steps).

1. I right-click on the assembly level sketch named "sketchDoor" and select edit sketch.

2. I then select "Project Geometry" for all 4 lines within the component sketch. (Also see attached CompSketch.jpg) which projects the geometry from the component to the assembly sketch (sketchDoor).  I then have a sketch in my assembly as shown in the attached file sketchDoor.jpg

3. I then close the sketch as I now have what I need.

 

Hope this clarifies things and I really appreciate the help.  As I mentioned in my first post, it is doing everything I want it to except for the most important part and that is projecting the geometry.

 

Thanks,

Keith

 

 

0 Likes
Message 5 of 8

adam.nagy
Autodesk Support
Autodesk Support

Hi Keith,

 

So you created geometry proxy for the sketch entities but the projection method still does not like them?

Could you paste your latest code?

 

Cheers,



Adam Nagy
Autodesk Platform Services
0 Likes
Message 6 of 8

keithjk
Advocate
Advocate

Adam,


Yes I did and I hate to admit, that my attempt included some "guess work".  I have placed a comment below where the program crashes.

 

Keith

 

----

 

Public Sub extrudeSketch()

'this checks to make sure Inventor is open and

'sets oAssemblyDocument to the active inventor document

CheckInventor()

'set some variables

Dim oDef As AssemblyComponentDefinition

oDef = oAssemblyDocument.ComponentDefinition

Dim oPartDef As ComponentDefinition

Dim oOcc As ComponentOccurrence

Dim oOccs As ComponentOccurrences = oDef.Occurrences

Dim sk As Sketch = Nothing

Dim skEnt As SketchEntity = Nothing

Dim skEntNew As SketchEntity = Nothing

Dim oSketchLine As SketchLine = Nothing

'sketch door is where the new sketch geometry will be placed

Dim sketchDoor As PlanarSketch = oAssemblyDocument.ComponentDefinition.Sketches.Item("sketchDoor")

'oSketchProxy

Dim oSketchProxy As PlanarSketchProxy = Nothing

Dim oTG As TransientGeometry

oTG = oInventorApp.TransientGeometry

'loop through all of the components in the assembly

For Each oOcc In oOccs

oPartDef = oOcc.Definition

'loop through all of the sketches in the component

'search for sketch name "sk_Cutout"

For Each sk In oPartDef.Sketches

If sk.Name = "sk_Cutout" Then

'we have found the sketch we are looking for

Call oOcc.CreateGeometryProxy(sk, oSketchProxy)

'/////////// here is where the program crashes ///////////////////

Call oDef.CreateGeometryProxy(oSketchProxy, oSketchProxy)

End If

Next

Next

End Sub

0 Likes
Message 7 of 8

adam.nagy
Autodesk Support
Autodesk Support

Hi,

 

Guessing is a good starting point 🙂

 

But it's the sketch line that I would try to create the proxy for. The "oSketchLine" object in your original code.

 

Cheers,



Adam Nagy
Autodesk Platform Services
0 Likes
Message 8 of 8

keithjk
Advocate
Advocate
Accepted solution

Adam,


Thank you!!!!  It is working!!!!  I have some cleanup to do and I want to add more functionality but it is now projecting the geometry.

 

Again, thank you.  I was getting very frustrated with this.

 

Keith

 

Public Sub extrudeSketch()

'this checks to make sure Inventor is open and

'sets oAssemblyDocument to the active inventor document

CheckInventor()

'set some variables

Dim oDef As AssemblyComponentDefinition

oDef = oAssemblyDocument.ComponentDefinition

Dim oPartDef As ComponentDefinition

Dim oOcc As ComponentOccurrence

Dim oOccs As ComponentOccurrences = oDef.Occurrences

Dim sk As Sketch = Nothing

Dim skEnt As SketchEntity = Nothing

Dim skEntNew As SketchLine = Nothing

Dim oSketchLine As SketchLine = Nothing

'sketch door is where the new sketch geometry will be placed

Dim sketchDoor As PlanarSketch = oAssemblyDocument.ComponentDefinition.Sketches.Item("sketchDoor")

'oSketchProxy

Dim oSketchProxy As SketchLineProxy = Nothing

Dim oTG As TransientGeometry

oTG = oInventorApp.TransientGeometry

'loop through all of the components in the assembly

For Each oOcc In oOccs

oPartDef = oOcc.Definition

'loop through all of the sketches in the component

'search for sketch name "sk_Cutout"

For Each sk In oPartDef.Sketches

If sk.Name = "sk_Cutout" Then

'we have found the sketch we are looking for

For Each skEnt In sk.SketchEntities

If skEnt.Type = ObjectTypeEnum.kSketchLineObject Then

oSketchLine = skEnt

Call oOcc.CreateGeometryProxy(oSketchLine, oSketchProxy)

skEntNew = sketchDoor.AddByProjectingEntity(oSketchProxy)

End If

Next

End If

Next

Next

End Sub

0 Likes