- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hey guys,
I am working on generating .ipt's from VBA. I am looking for a chunk of code to extrude a sketch. Also, how do I distinguish which region I want to extrude (ex. a donut-ring or middle).
Any help would be greatly appreciated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
hello rcolon9E4ZX,
here is a bit code from the API inventor help, look for extrude, and look at the examples:
Public Sub DrawBlockWithPocket()
' Create a new part document, using the default part template.
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, _
ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject))
' Set a reference to the component definition.
Dim oCompDef As PartComponentDefinition
Set oCompDef = oPartDoc.ComponentDefinition
' Create a new sketch on the X-Y work plane. Since it's being created on
' one of the base workplanes we know the orientation it will be created in
' and don't need to worry about controlling it. Because of this we also
' know the origin of the sketch plane will be at (0,0,0) in model space.
Dim oSketch As PlanarSketch
Set oSketch = oCompDef.Sketches.Add(oCompDef.WorkPlanes(3))
' Set a reference to the transient geometry object.
Dim oTransGeom As TransientGeometry
Set oTransGeom = ThisApplication.TransientGeometry
' Draw a 4cm x 3cm rectangle with the corner at (0,0)
Dim oRectangleLines As SketchEntitiesEnumerator
Set oRectangleLines = oSketch.SketchLines.AddAsTwoPointRectangle( _
oTransGeom.CreatePoint2d(0, 0), _
oTransGeom.CreatePoint2d(4, 3))
' Create a profile.
Dim oProfile As Profile
Set oProfile = oSketch.Profiles.AddForSolid
' Create a base extrusion 1cm thick.
Dim oExtrudeDef As ExtrudeDefinition
Set oExtrudeDef = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kJoinOperation)
Call oExtrudeDef.SetDistanceExtent(1, kNegativeExtentDirection)
Dim oExtrude As ExtrudeFeature
Set oExtrude = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef)
' Get the top face of the extrusion to use for creating the new sketch.
Dim oFrontFace As Face
Set oFrontFace = oExtrude.StartFaces.Item(1)
' Create a new sketch on this face, but use the method that allows you to
' control the orientation and orgin of the new sketch.
Set oSketch = oCompDef.Sketches.AddWithOrientation(oFrontFace, _
oCompDef.WorkAxes.Item(1), True, True, oCompDef.WorkPoints(1))
' Determine where in sketch space the point (0.5,0.5,0) is.
Dim oCorner As Point2d
Set oCorner = oSketch.ModelToSketchSpace(oTransGeom.CreatePoint(0.5, 0.5, 0))
' Create the interior 3cm x 2cm rectangle for the pocket.
Set oRectangleLines = oSketch.SketchLines.AddAsTwoPointRectangle( _
oCorner, oTransGeom.CreatePoint2d(oCorner.X + 3, oCorner.Y + 2))
' Create a profile.
Set oProfile = oSketch.Profiles.AddForSolid
' Create a pocket .25 cm deep.
Set oExtrudeDef = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kCutOperation)
Call oExtrudeDef.SetDistanceExtent(0.25, kNegativeExtentDirection)
Set oExtrude = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef)
End Sub
Kudo's are also appreciated
Succes on your project, and have a nice day
Herm Jan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi @HermJan.Otterman
Can we draw two rectangles to same sketch and create profile by selecting just outer rectangle and leaving inner rectangle ? I mean, instead of creating two sketch, can we work with just one sketch ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello @Mehmet_Fatih_Turker,
yes you can. it is basically all in the help of Inventor.
so if you use the addforsolid, you can specify some optional input.
If you want the whole sketch to be extruded then you don't put in anything, but in your case you can.
so create the two rectangles, (oRectangleLines3, and oRectangleLines4)... (names in the code)
then in the addforsolid you can only pass an objectcollection, so create the object collecction and add only the largest rectangle to it.
then if you create te profile, pass in the object colletion.
try you self to change the first option from false tot true (should be false)
the code:
Public Sub Main() ' Check to make sure a sketch is open. If Not TypeOf ThisApplication.ActiveEditObject Is PlanarSketch Then MsgBox ("A sketch must be active.") Exit Sub End If ' Set a reference to the active sketch. Dim oSketch As PlanarSketch oSketch = ThisApplication.ActiveEditObject ' Set a reference to the transient geometry collection. Dim oTransGeom As TransientGeometry oTransGeom = ThisApplication.TransientGeometry Dim oRectangleLines3 As SketchEntitiesEnumerator oRectangleLines3 = oSketch.SketchLines.AddAsTwoPointCenteredRectangle( _ oTransGeom.CreatePoint2d(0, 0), _ oTransGeom.CreatePoint2d(5, 5)) Dim oRectangleLines4 As SketchEntitiesEnumerator oRectangleLines4 = oSketch.SketchLines.AddAsTwoPointCenteredRectangle( _ oTransGeom.CreatePoint2d(0, 0), _ oTransGeom.CreatePoint2d(6, 6)) Dim partDoc As PartDocument = ThisDoc.Document Dim oCompDef = partDoc.ComponentDefinition Dim objcol = ThisApplication.TransientObjects.CreateObjectCollection(oRectangleLines4) Dim oProfile As Profile oProfile = oSketch.Profiles.AddForSolid(False, objcol) ' Create a base extrusion 1cm thick. Dim oExtrudeDef As ExtrudeDefinition oExtrudeDef = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kJoinOperation) oExtrudeDef.SetDistanceExtent(1, kNegativeExtentDirection) Dim oExtrude As ExtrudeFeature oExtrude = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef) End Sub
succes
Kudo's are also appreciated
Succes on your project, and have a nice day
Herm Jan