Inventor Customization

Inventor Customization

Reply
Valued Mentor
346 Posts
9 Kudos
Registered: ‎04-10-2010
Post 1 of 7
Accepted Solution

add sketch on part's face by vba

898 Views, 6 Replies
10-22-2012 01:32 PM

hi,

 

I can add sketch on a part's workplane (xy, xz, yz) by using sketchs.add.

 

is there anyway i can select a face of part and create a new sketch?

 

thanks for any tips.

 

Autodesk Inventor Professional 2014
Autodesk Product Design Suite Standard 2014
Microsoft Office 2007 & 2013
Windows 7 64bit
Synergis Adept 2014
Visual Studio Express 2010

Here's one method I use on Extruded parts, but hopefully you can gleem enough from it.

 

Sub Sketch2Face2()
'J.Kriek 2012
Dim oApp As Application
Set oApp = ThisApplication
Dim oPart As PartDocument
Set oPart = oApp.ActiveDocument
Dim oCompDef As ComponentDefinition
Set oCompDef = oPart.ComponentDefinition
Dim oExtrude As ExtrudeFeatures
Set oExtrude = oCompDef.Features.ExtrudeFeatures
Dim oFaces As Faces
'This can be changed to .SideFaces, .EndFaces, or .StartFaces
'But .Faces will let you choose between all faces
Set oFaces = oExtrude.Item(1).Faces
Dim oSketch As PlanarSketch
'Put a sketch on the first face (1) - change to suit
Set oSketch = oCompDef.Sketches.Add(oFaces(1), True)
End Sub

 

*Expert Elite*
546 Posts
106 Kudos
Registered: ‎03-29-2007
Post 2 of 7

Re: add sketch on part's face by vba

10-23-2012 09:07 AM in reply to: jcneal

Here's one method I use on Extruded parts, but hopefully you can gleem enough from it.

 

Sub Sketch2Face2()
'J.Kriek 2012
    Dim oApp As Application
    Set oApp = ThisApplication
    Dim oPart As PartDocument
    Set oPart = oApp.ActiveDocument
    Dim oCompDef As ComponentDefinition
    Set oCompDef = oPart.ComponentDefinition
    Dim oExtrude As ExtrudeFeatures
    Set oExtrude = oCompDef.Features.ExtrudeFeatures
    Dim oFaces As Faces
    
    'This can be changed to .SideFaces, .EndFaces, or .StartFaces
    'But .Faces will let you choose between all faces
    Set oFaces = oExtrude.Item(1).Faces
    
    Dim oSketch As PlanarSketch

    'Put a sketch on the first face (1) - change to suit
    Set oSketch = oCompDef.Sketches.Add(oFaces(1), True)
End Sub

 

Jonathan D. Kriek
Manufacturing Solutions Engineer
KETIV Technologies, Inc.



_____________________________________________________
Did I help you? Please choose Accept as Solution or Kudos below
Valued Mentor
346 Posts
9 Kudos
Registered: ‎04-10-2010
Post 3 of 7

Re: add sketch on part's face by vba

10-24-2012 09:41 AM in reply to: jdkriek

thanks a lot.

 

I modified your code to fit my need. It works fine.

 

I don't understand how you get .sidefaces, endfaces to work. I have to test few times to get face right. in thise case is oFace(4)

 

Is there any way I always get top face of a face feature in sheet metal part?

Autodesk Inventor Professional 2014
Autodesk Product Design Suite Standard 2014
Microsoft Office 2007 & 2013
Windows 7 64bit
Synergis Adept 2014
Visual Studio Express 2010
*Expert Elite*
546 Posts
106 Kudos
Registered: ‎03-29-2007
Post 4 of 7

Re: add sketch on part's face by vba

10-25-2012 06:35 AM in reply to: jcneal

SideFaces, EndFaces, ect is only available for extruded features. When dealing with sheetmetal faces features you can only choose the item number. And yes unfortunately it's a guessing game as to which number that is depending on how the face was created.

 

However, I did have a crazy idea...

 

You can obtain the .BaseFace from the flat pattern and since it's a face feature it's going to be the top face. It will create the sketch in the folded. This worked for me, but try it out.

 

Sub Sketch2FlatFace()
'J.Kriek 2012
    Dim oApp As Application
    Set oApp = ThisApplication
    Dim oPart As PartDocument
    Set oPart = oApp.ActiveDocument

    Dim oSheetMetalComp As SheetMetalComponentDefinition
    Set oSheetMetalComp = oPart.ComponentDefinition
    
    'Make sure there is a flat pattern
    If oSheetMetalComp.HasFlatPattern = False Then
    
        'Create flat pattern
        Call oSheetMetalComp.Unfold
        
        'Switch back to Folded
        Set oDef = oApp.CommandManager.ControlDefinitions.Item("PartSwitchRepresentationCmd")
        Call oDef.Execute
    End If
    
    Dim oFlatpattern As FlatPattern
    Set oFlatpattern = oSheetMetalComp.FlatPattern

    'Edit the flat pattern
    Call oFlatpattern.Edit

    'Grab the BaseFace (Top) - can also be .BottomFace
    Dim oFace As Face
    Set oFace = oFlatpattern.BaseFace
    
    'Exit the flat pattern
    Call oFlatpattern.ExitEdit
    
    'Sketch on Top Face
    Dim oSketch As PlanarSketch
    Set oSketch = oSheetMetalComp.Sketches.Add(oFace, True)
    
    'Remove the flat pattern - optional
    'Call oFlatpattern.Delete
End Sub
Jonathan D. Kriek
Manufacturing Solutions Engineer
KETIV Technologies, Inc.



_____________________________________________________
Did I help you? Please choose Accept as Solution or Kudos below
Valued Mentor
346 Posts
9 Kudos
Registered: ‎04-10-2010
Post 5 of 7

Re: add sketch on part's face by vba

10-25-2012 08:51 AM in reply to: jdkriek

well, mine is front face. so the sketch goes to my flange side whick is top face.

 

I did face feature on x-y plane.

 

thanks a lot. I learn something from it.

Autodesk Inventor Professional 2014
Autodesk Product Design Suite Standard 2014
Microsoft Office 2007 & 2013
Windows 7 64bit
Synergis Adept 2014
Visual Studio Express 2010
Mentor
258 Posts
33 Kudos
Registered: ‎09-17-2010
Post 6 of 7

Re: add sketch on part's face by vba

09-10-2013 10:20 AM in reply to: jdkriek

how to execute the 2d sketch command in vba?

Mentor
258 Posts
33 Kudos
Registered: ‎09-17-2010
Post 7 of 7

Re: add sketch on part's face by vba

09-10-2013 11:06 AM in reply to: jcneal

i want to select a face from a derived solid and project geometry to get a sketch.

 

can anybody help me with this?

 

thanks:smileyhappy:

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!