Replace ExtrudeDefinition Profile leads to Exception

Replace ExtrudeDefinition Profile leads to Exception

PierreMJ
Explorer Explorer
239 Views
3 Replies
Message 1 of 4

Replace ExtrudeDefinition Profile leads to Exception

PierreMJ
Explorer
Explorer

Hi, quite new to Inventor programming'. I have following problem (C# AddIn project), thank you for your help:

I want to replace the profile of an existing extrude. As a first try, I just want to reproduce that sample: https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=ExtrudeFeature_Profile_Sample

 

Meaning:

 

1- create a rectangle in the existing sketch:

var pt1 = inventorApplication.TransientGeometry.CreatePoint2d(0, 0);
var pt2 = inventorApplication.TransientGeometry.CreatePoint2d(30, 10);
var lines = sketch.SketchLines.AddAsTwoPointRectangle(pt1, pt2);

 

2- create the object collection:

var pathSegments = inventorApplication.TransientObjects.CreateObjectCollection();
foreach (var line in lines)

      pathSegments.Add(line);

 

3- create the new profile and reset the extrude definition:

var profile = sketch.Profiles.AddForSolid(false, pathSegments);
myExtrude.Definition.Profile = profile;

 

Actually basic. But I get an exception at the last line (Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))). Any ideas what's going wrong? Thank you.

0 Likes
240 Views
3 Replies
Replies (3)
Message 2 of 4

Stakin
Collaborator
Collaborator
Dim oPartDoc As PartDocument
oPartDoc = ThisApplication.ActiveDocument            
Dim oCompDef As PartComponentDefinition
oCompDef = oPartDoc.ComponentDefinition
MessageBox.Show("Create oExtrude1", "Title")
Dim oSketch As PlanarSketch
oSketch = oCompDef.Sketches.Add(oCompDef.WorkPlanes(3))
Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry
Dim oRectangleLines As SketchEntitiesEnumerator
oRectangleLines = oSketch.SketchLines.AddAsTwoPointRectangle( _
                            oTG.CreatePoint2d(0, 0), _
                            oTG.CreatePoint2d(4, 3))

Dim oProfile As Profile
oProfile = oSketch.Profiles.AddForSolid   
Dim oExtrudeDef As ExtrudeDefinition
oExtrudeDef = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kJoinOperation)
Call oExtrudeDef.SetDistanceExtent(1, kNegativeExtentDirection)
Dim oExtrude1 As ExtrudeFeature

oExtrude1 = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef)
'Edit oExtrude1 sketch
MessageBox.Show("Edit oExtrude1 sketch", "Title")

oSketch.Edit
oRectangleLines = oSketch.SketchLines.AddAsTwoPointRectangle( _
                            oTG.CreatePoint2d(-1, -2), _
                            oTG.CreatePoint2d(-5, -6))
oSketch.ExitEdit
oProfile = oSketch.Profiles.AddForSolid
MessageBox.Show("After Edit oExtrude1 sketch", "Title")
oExtrude1.Definition.Profile = oProfile
MessageBox.Show("Create oExtrude2", "Title")
Dim oExtrude2 As ExtrudeFeature
Dim oFrontFace As Face
oFrontFace = oExtrude1.StartFaces.Item(1)
oSketch = oCompDef.Sketches.AddWithOrientation(oFrontFace, _
                oCompDef.WorkAxes.Item(1), True, True, oCompDef.WorkPoints(1))
                
Dim oCorner As Point2d
oCorner = oSketch.ModelToSketchSpace(oTG.CreatePoint(0.5, 0.5, 0))
oRectangleLines = oSketch.SketchLines.AddAsTwoPointRectangle( _
            oCorner, oTG.CreatePoint2d(oCorner.X + 3, oCorner.Y + 2))
oProfile = oSketch.Profiles.AddForSolid
oExtrudeDef = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kCutOperation)
Call oExtrudeDef.SetDistanceExtent(0.25, kNegativeExtentDirection)
oExtrude2 = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef)
'Edit oExtrude2 sketch
MessageBox.Show("Edit oExtrude2 sketch", "Title")
oSketch.Edit
oRectangleLines = oSketch.SketchLines.AddAsTwoPointRectangle( _
                            oTG.CreatePoint2d(2, 2), _
                            oTG.CreatePoint2d(5, 5))
oSketch.ExitEdit
oProfile = oSketch.Profiles.AddForSolid
MessageBox.Show("After Edit oExtrude2 sketch", "Title")
oExtrude2.Definition.Profile = oProfile
0 Likes
Message 3 of 4

PierreMJ
Explorer
Explorer

Hi,

thank you, for the answer. May you explicit what exactly I have to pay attention to in that sample?

0 Likes
Message 4 of 4

Stakin
Collaborator
Collaborator

If you just want use the profile of new create sketchlines,you can run the sample of https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=ExtrudeFeature_Profile_Sample

 

 

 Dim oPartDoc As PartDocument
    oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, _
    ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject))

    ' Set a reference to the component definition.
    Dim oCompDef As PartComponentDefinition
    oCompDef = oPartDoc.ComponentDefinition

    ' Create a new sketch on the X-Y work plane.
    Dim oSketch As PlanarSketch
    oSketch = oCompDef.Sketches.Add(oCompDef.WorkPlanes(3))

    ' Set a reference to the transient geometry object.
    Dim oTransGeom As TransientGeometry
    oTransGeom = ThisApplication.TransientGeometry

    Dim oCenter As Point2d
    oCenter = oTransGeom.CreatePoint2d(-5, 0)

    ' Create a sketch circle
    Dim oCircle As SketchCircle
    oCircle = oSketch.SketchCircles.AddByCenterRadius(oCenter, 1)

    ' Create a profile based on the circle
    Dim oProfile As Profile
    oProfile = oSketch.Profiles.AddForSolid

    ' Create a base extrusion 4 cm thick.
    Dim oExtrudeDef As ExtrudeDefinition
    oExtrudeDef = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kJoinOperation)
    Call oExtrudeDef.SetDistanceExtent(4, kPositiveExtentDirection)
    Dim oExtrude As ExtrudeFeature
    oExtrude = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef)
    
    ' Draw a 4cm x 3cm rectangle with the corner at (0,0)
    Dim oRectangleLines As SketchEntitiesEnumerator
    oRectangleLines = oSketch.SketchLines.AddAsTwoPointRectangle( _
    oTransGeom.CreatePoint2d(0, 0), _
    oTransGeom.CreatePoint2d(3, 1))

    ' Add all the lines of the rectangle to an ObjectCollection
    Dim oPathSegments As ObjectCollection
    oPathSegments = ThisApplication.TransientObjects.CreateObjectCollection

    Dim oEntity As SketchEntity
    For Each oEntity In oRectangleLines
      oPathSegments.Add(oEntity)
    Next

    ' Create a profile that represents the newly created rectangle
    ' and excludes the original circle.
    oProfile = oSketch.Profiles.AddForSolid(False, oPathSegments)

    ' Set the new profile
    oExtrude.Definition.Profile = oProfile

 

 

if you want use all the sketch as the profile, You can use the code from my previous post.

0 Likes