Hello;
My question is about the Inventor API: how can I update the content of a sketch (entities & constraints) using the API while the sketch itself is already used in a feature? Going through the interface, I can easily open up a loft or an extrusion, start editing its sketch, and replace e.g. an arc segment by a line segment to alter the profile. However, through the API, trying to delete old sketch entities or add new entities to the sketch before deleting the old ones either fails with an HR code that I cannot comprehend, or Inventor just crashes/hangs on resolving the part (with an empty error message box...?).
I understand how feature parameters can be updated (e.g. the distance of an extrusion or the angle of a draft) and I can do that fine, but updating the sketches seems a lot more complicated. I suspect I might have to recreate profiles each time sketches require modifications and update the features with these new profiles, but why would the sketch itself seem 'locked' through the API? I have tried encompassing the modfication calls between 'Edit'/'ExitEdit' and EndOfPart barriers, to no avail.
Is there a sample somewhere that I have missed?
FYI, I am programming an addin in C++ through COM interfaces.
Hi pierre-luc.st-charles,
Here's a VBA example from the API help files.
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
Edit profile of an extrude feature API Sample Description This sample demonstrates editing the profile of an extrude feature. Code Samples VBA Sample Code Public Sub EditFeatureProfile() ' 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. 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 Dim oCenter As Point2d Set oCenter = oTransGeom.CreatePoint2d(-5, 0) ' Create a sketch circle Dim oCircle As SketchCircle Set oCircle = oSketch.SketchCircles.AddByCenterRadius(oCenter, 1) ' Create a profile based on the circle Dim oProfile As Profile Set oProfile = oSketch.Profiles.AddForSolid ' Create a base extrusion 4 cm thick. Dim oExtrudeDef As ExtrudeDefinition Set oExtrudeDef = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kJoinOperation) Call oExtrudeDef.SetDistanceExtent(4, kPositiveExtentDirection) Dim oExtrude As ExtrudeFeature Set oExtrude = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef) ' 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)) ' Add all the lines of the rectangle to an ObjectCollection Dim oPathSegments As ObjectCollection Set 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. Set oProfile = oSketch.Profiles.AddForSolid(False, oPathSegments) ' Set the new profile oExtrude.Definition.Profile = oProfile End Sub
Thanks for the reply;
I have stumbled upon this sample before. It explains how to replace the profile used in a feature by using new sketch entities added in the orignal profile's sketch (which is pretty useful!), but it does not address how to remove (delete) entities in the original sketch.
Basically, in the case of that sample, I would be looking to erase the SketchCircle entity that was previously created (and used to create a profile) as my first step, then add more entities (like the rectangle) to the sketch to replace it, and finally create a new profile via oSketch.Profiles.AddForSolid. So far, my attempts all fail because I am not be able to delete the "circle entity" if it is already used in a profile, and keeping an object collection of new entities (e.g. like the rectangle lines) is unfeasible.
Any ideas?
Hi pierre-luc.st-charles,
I don't have time to experiment with this at the moment, but what about this example?
See the "Modify the Profile" section.
I'll try and have a look again tomorrow as well.
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
Sketch profile control API Sample Description This sample demonstrates the usage of the Profiles API to control the shape of the profile. The sample creates three concntric circles and creates an extrusion of the region between the inner circles. Code Samples VBA Sample Code Public Sub SketchProfileControl() ' 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 3 concentric circles. Dim oCircle1 As SketchCircle Set oCircle1 = oSketch.SketchCircles.AddByCenterRadius( _ oTransGeom.CreatePoint2d(5, 5), 6) Dim oCircle2 As SketchCircle Set oCircle2 = oSketch.SketchCircles.AddByCenterRadius( _ oTransGeom.CreatePoint2d(5, 5), 4) Dim oCircle3 As SketchCircle Set oCircle3 = oSketch.SketchCircles.AddByCenterRadius( _ oTransGeom.CreatePoint2d(5, 5), 2) ' Create a profile. Dim oProfile As Profile Set oProfile = oSketch.Profiles.AddForSolid ' Modify the profile: the returned profile consists of 3 ' paths each corresponding to a sketch circle. The desired ' result is that the innermost path removes material and the ' second path adds material. The outermost path is not needed ' and is hence deleted. Dim oProfPath As ProfilePath For Each oProfPath In oProfile If oProfPath.Item(1).SketchEntity Is oCircle3 Then oProfPath.AddsMaterial = False ElseIf oProfPath.Item(1).SketchEntity Is oCircle2 Then oProfPath.AddsMaterial = False Else oProfPath.Delete End If Next ' 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) 'Make the sketch visible for better visualization oSketch.Visible = True End Sub
I don
Now this gives me some more ideas! If removing the entities from the sketch does not work, maybe updating the profiles directly (by removing and adding profile paths) like what is shown in this sample might work. I would assume the original entities would not be affected if the paths are removed, but that is not too important to me.
I'll have to re-test all of this after the holidays; I'll update this post if I find a good solution.
Thanks!
Can you post a model or at least of picture of the before and after of the sketch you're modifying? I just tried a very simple case where I had a sketch containing a ellipse and a circle and an extrusion that was based on the ellipse. My program then deleted the ellipse, created a new profile using the circle, and assigned this new profile to the extrude. Everything worked, including the delete of the ellipse even though it was used by the extrusion.