By Adam Nagy
Creating a Sweep Feature is usually quite straight forward. However, you can run into some issues - like the one I'm going to talk about now.
If you have two sketches, one used as the Profile (Sketch2) and one used as the Path (Sketch1), then if they are connected through a sketch point then the CreatePath() function will use all the entities from both sketches and your swept surface creation will fail. Here is the debug message from the below code:
Sketch2 -4.9; 3.29922145229642; .15 -4.9; -3.29922145229644; .15 Sketch1 -4.9; -3.29922145229644; .15 -5.05; -3.29922145229644; .15 Sketch1 -5.05; -3.29922145229644; .15 -5.35; -3.29922145229644; .45 Sketch1 -5.35; -3.29922145229644; .45 -5.35; -3.29922145229644; 2.35
You'll find the same behaviour in the UI: if you're trying to select the entities for the Path first and click an entity in Sketch1, then not only the Sketch1 entities will be selected for the Path, but also the Sketch2 entity which is connected to a Sketch1 entity through a sketch point:
If I select the Profile first by clicking the Sketch2 entity, and then try to select the Path, then Inventor is clever enough not to use the Profile entity as part of the Path:
When doing things programmatically, we need to add a similar logic to our code: remove the Sketch2 entity from the Path.
The easiest way to do that is to use CreatePath() to collect all the entities for the Path, then collect from that all the entities which are inside Sketch1. Then you can call CreateSpecifiedPath() to create a path that is using the provided entities and in the order they were provided.
Note: CreateSpecifiedPath() seems to require you to pass the entities in the order you want to use them ensuring a continuous path, and providing them in the wrong order will make it fail: e.g. passing in Line1, Line3, Line2 will fail. That's why letting Inventor figure out the right order by using CreatePath() first seems to be the easiest solution.
Sub PrintPoint(p As Point) Debug.Print Str(p.X) + "; " + Str(p.Y) + "; " + Str(p.Z) End Sub Sub PrintPathInfo(p As Path) Dim pe As PathEntity For Each pe In p Debug.Print pe.SketchEntity.Parent.Name PrintPoint pe.Curve.StartPoint PrintPoint pe.Curve.EndPoint Next End Sub Sub CreateSweep() ' Before running this code select first Sketch1 and then ' Sketch2 as well in the UI Dim oDoc As PartDocument Set oDoc = ThisApplication.ActiveDocument Dim oCompDef As PartComponentDefinition Set oCompDef = oDoc.ComponentDefinition Dim oSkPath As PlanarSketch Set oSkPath = oDoc.SelectSet(1) Dim oSkProfile As PlanarSketch Set oSkProfile = oDoc.SelectSet(2) ' Just to clean up after all the tests Do While oSkProfile.Profiles.Count > 0 oSkProfile.Profiles(1).Delete Loop Dim oProfile As Profile Set oProfile = oSkProfile.Profiles.AddForSurface() Dim oPath As Path Set oPath = oCompDef.Features.CreatePath(oSkPath.SketchLines(1)) ' This might contain entities from other sketches Debug.Print "Default Path" Call PrintPathInfo(oPath) ' Remove unnecessary lines from it Dim oTO As TransientObjects Set oTO = ThisApplication.TransientObjects Dim oColl As ObjectCollection Set oColl = oTO.CreateObjectCollection Dim oPathEntity As PathEntity For Each oPathEntity In oPath Dim oSkEntity As SketchEntity Set oSkEntity = oPathEntity.SketchEntity If oSkEntity.Parent Is oSkPath Then Call oColl.Add(oSkEntity) End If Next Dim oPathNew As Path Set oPathNew = oCompDef.Features.CreateSpecifiedPath(oColl) Debug.Print "New Path" Call PrintPathInfo(oPathNew) Dim oSweepDef As SweepDefinition Set oSweepDef = oCompDef.Features.SweepFeatures.CreateSweepDefinition( _ kPathSweepType, oProfile, oPathNew, kSurfaceOperation) Dim oSweepOne As SweepFeature Set oSweepOne = oCompDef.Features.SweepFeatures.Add(oSweepDef) End Sub
VBA Immediate Window with our debug strings:
Default Path Sketch2 -4.9; 3.29922145229642; .15 -4.9; -3.29922145229644; .15 Sketch1 -4.9; -3.29922145229644; .15 -5.05; -3.29922145229644; .15 Sketch1 -5.05; -3.29922145229644; .15 -5.35; -3.29922145229644; .45 Sketch1 -5.35; -3.29922145229644; .45 -5.35; -3.29922145229644; 2.35 New Path Sketch1 -4.9; -3.29922145229644; .15 -5.05; -3.29922145229644; .15 Sketch1 -5.05; -3.29922145229644; .15 -5.35; -3.29922145229644; .45 Sketch1 -5.35; -3.29922145229644; .45 -5.35; -3.29922145229644; 2.35
And the result: