add sketch on a face in assembly, API

add sketch on a face in assembly, API

Anonymous
Not applicable
2,540 Views
7 Replies
Message 1 of 8

add sketch on a face in assembly, API

Anonymous
Not applicable

I am working an Application on Inventor. I need to select a face of a part in assembly and create a sketch on it.

The sketches.add command works fine on a Work Plane, but always error with a selected face.

Here is part of my code, i can't figure out where is worry with it

 

Dim assDoc As AssemblyDocument = oapp.ActiveDocument
Dim assdocdef As AssemblyComponentDefinition
assdocdef = assDoc.ComponentDefinition

             ' Call the Pick method and set the filter to pick any face.

Dim oSelect As New Class1(oapp)

Dim oFace As Face
oFace = oSelect.Pick(SelectionFilterEnum.kPartFaceFilter)

             'start edit mode on the first part in assembly
Dim oOcc As ComponentOccurrence = assdocdef.Occurrences.Item(1)
oOcc.Edit()

            
            ' access the part document component definition
Dim oPartCompDef As PartComponentDefinition = oOcc.Definition

            ' create a new sketch
Dim oSketch As Sketch

            'the code below can't work
'oSketch = oPartCompDef.Sketches.Add(oFace)

            'the code below can't work too 
'oSketch = assDoc.ComponentDefinition.Sketches.Add(oFace)

        'but it works good if i select a work plane
oSketch = oPartCompDef.Sketches.Add(oPartCompDef.WorkPlanes.Item("Work Plane1"))
        oSketch.Edit()

Maybe because Inventor can not know the face belong to the part? I have already tried a few days but still can't fix it.

 

 

2. And another question pls, it that possible, for example:

Here is a cube, and of course with 6 faces, named face 1 to face 6

For each face there is a sketch on it, called sketch1 to sketch 6

Is that possible, if i pick face 1 and want to sketch something on it, the programm will know, i selected face 1 and there is already sketch 1 on there.

So it will run the "edit sketch" command on sketch 1 instead of create a new sketch?

 

Thanks very much for your help!!

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

MechMachineMan
Advisor
Advisor

You should look more into proxies.

 

As you are running the rule from an assembly, I think that when you run pick face on the part, you are actually returned with a face proxy; the face in the context of the assembly.

 

Given that, inventor can't create a sketch on a part given a face in an assembly. 

 

You will need to use the proxy object to find the parent face within the context of the part. Good luck!

 

See the code I posted in your cylinder/rod/piston constraint thread last time for an example of proxy usage


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 3 of 8

Anonymous
Not applicable

thanks again for your reply.I will try to solution it. hope i can figure it out 

0 Likes
Message 4 of 8

ekinsb
Alumni
Alumni
Accepted solution

Justin is correct.  The problem you're having is because of context.

 

Here's a VBA version of your code that I did for a quick test.  This creates a sketch inside the part where the selected face is.  I don't know if you're activating things because you want the user to do something, but if you're just using the API you don't need to activate something to be able to create stuff inside it. 

 

You also said that in the last line where you try to create the sketch in the assembly is failing.  It shouldn't be because the input face is in the context of the assembly and it's working in the sample below.

 

Public Sub CreateSketchTest()
    Dim assDoc As AssemblyDocument
    Set assDoc = ThisApplication.ActiveDocument
    Dim assDocDef As AssemblyComponentDefinition
    Set assDocDef = assDoc.ComponentDefinition

    ' Call the Pick method and set the filter to pick any face.
    Dim oFace As FaceProxy
    Set oFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, "Pick a face")

    'start edit mode on occurence the selected face is in.
    Dim oOcc As ComponentOccurrence
    Set oOcc = oFace.ContainingOccurrence
    'Call oOcc.Edit
            
    ' access the part document component definition
    Dim oPartCompDef As PartComponentDefinition
    Set oPartCompDef = oOcc.Definition

    ' create a new sketch on the selected face, in the context of the part.
    Dim oSketch As sketch
    Set oSketch = oPartCompDef.Sketches.Add(oFace.NativeObject)
    
    'the code below can't work too
    Set oSketch = assDoc.ComponentDefinition.Sketches.Add(oFace)
End Sub

 

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 5 of 8

Anonymous
Not applicable

Sincere thanks for your help, this is exactly want i need!!

The only thing in my programm is, the code "Call oOccc.Edit " must not be removed, otherwise the sketch will not open.

 

Thanks again, it's really helps a lot!!

 

0 Likes
Message 6 of 8

basnederveen
Advocate
Advocate

Hi sorry for reacting on an old post, but I wanted to accomplish something like this. Im trying to create a sketch on a face in an assembly. I want to use the pick command to obtain a faceproxy.  The last line of the code you provided Brian errors for me? (Method 'Add' of object 'PlanarSketches' failed)

 

What can be the problem? I was trying with my own code first but also when I tried yours the problem occurred.

 

 

0 Likes
Message 7 of 8

ekinsb
Alumni
Alumni

Sorry for the late reply.  I was busy with Autodesk University the past couple of weeks.

 

Before I can answer I need to better understand what you're trying to do. In which component do you want to create the sketch?  I suspect that's the problem.  A proxy typically represents an object in the context of the top-level root component, so if you're providing a selected face proxy to create a sketch, you should be creating the sketch in the root component.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 8 of 8

basnederveen
Advocate
Advocate

Hi Brian, thanks for your reply! I also do not completely remember what the problem was, it is solved though! 

0 Likes