Create Sketch Fom Selected Face (Replicate "Start 2D Sketch" tool)

Create Sketch Fom Selected Face (Replicate "Start 2D Sketch" tool)

kristian_oa
Enthusiast Enthusiast
553 Views
2 Replies
Message 1 of 3

Create Sketch Fom Selected Face (Replicate "Start 2D Sketch" tool)

kristian_oa
Enthusiast
Enthusiast

Hello,

 

I am trying to create a sketch onto a face within a part, which I will then add/modify features from, however I am unable to create a 2D sketch onto any desired face as I am given an error upon running Sketches.Add().

 

This part is currently an immediate child of the active assembly, but I have also attempted in modifying a part within an assembly, which itself is within the active assembly (a part within a sub-assembly).

 

To simplify my debugging process, I decided to replicate the "Start 2D Sketch" tool and select a specific face I want to create a sketch onto. Code snippets for this are below:

 

iLogic Sample:

Dim face As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Select a face")

' Only attempt to add a sketch if the selected face belongs to a part
If (face.Parent.ComponentDefinition.Type = ObjectTypeEnum.kPartComponentDefinitionObject) Then
	Dim def = face.Parent.ComponentDefinition
    Dim sketch As PlanarSketch = def.Sketches.Add(selection)
End If

C# Sample: 

Face selection = (Face)Globals.InventorApp.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Select a face");

// Only attempt to add a sketch if the selected face belongs to a part
if (((Face)selection).Parent.ComponentDefinition is PartComponentDefinition def)
{
    PlanarSketch profile = def.Sketches.Add(selection);
}

 

Both of these snippets fail upon adding the sketch the part's component definition.

What am I doing wrong?

 

To my understanding, you can only create a sketch profile on a planar face that resides within its part. But the code above is accessing the component definition of the face as it's parent, ensuring that I am getting the correct component definition for the face.

 

Are faces not suitable for sketches? In Inventor's built-in "Start 2D Sketch" tool you can select a face on some geometry to create a sketch onto.

 

Any guidance into understanding why I cannot add a sketch to a face of a part would be greatly appreciated.

0 Likes
Accepted solutions (1)
554 Views
2 Replies
Replies (2)
Message 2 of 3

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @kristian_oa 
Quick fix:

Dim face As FaceProxy = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Select a face")

' Only attempt to add a sketch if the selected face belongs to a part
If (face.Parent.ComponentDefinition.Type = ObjectTypeEnum.kPartComponentDefinitionObject) Then
	Dim def = face.Parent.ComponentDefinition
    Dim sketch As PlanarSketch = def.Sketches.Add(face.NativeObject)
End If

When you pick a face in assembly environment it's actually not a face but a faceproxy (the face represented in context of the assembly). Therefore, use face.NativeObject to get the face in the parts context 🙂

Message 3 of 3

kristian_oa
Enthusiast
Enthusiast

Thank you so much for this!

I completely forgot proxies exist and are to be used in these scenarios. You're a legend!