By Adam Nagy
If he have the above model then this is how we get from the basic geometry to the solid model in the derived part. We start with the SketchBlockDefinition (2c2x.255) inside our PartDocument (SquareTubeSteelR!.ipt), an instance of which we place inside a PlanarSketch (Sketch2). Then based on that a Profile is created that is used by the ExtrudeFeature (Extrusion2) and that generates a SurfaceBody (Solid2). Now we derived another part from it (Solid2.ipt) and imported that solid from the model and got the SurfaceBody (Solid1) in the derived part.
As always, it's very useful to select various entities in the UI and then poke around in the VBA Watches window to see their properties: http://adndevblog.typepad.com/manufacturing/2013/10/discover-object-model.html
You can check what got brought over from the original part through the ReferenceComponents enumarator of the PartComponentDefinition. Inside that you'll find all the things that are derived from a PartDocument via DerivedPartComponents. We can go through the SolidBodies enumaration to get the solids inside the derived part which come from another part. Then we can get to the original geometry inside the original part using ReferencedEntity.
Now we are inside the original part (SquareTubeSteelR!.ipt) and can drill down further. We can find out what feature created the SurfaceBody through CreatedByFeature, then get the Profile that the feature used. That will give back the SketchEntity's that it is based on, and from their ContainingSketchBlock property we can tell if they originally came from an instance of a SketchBlockDefinition.
Here is a sample VBA code. If we did not know how exactly the model is built up we'd need many more if/else blocks. Run this inside the derived part (Solid2.ipt in our case)
Sub GetSketchBlock() Dim pt As PartDocument Set pt = ThisApplication.ActiveDocument Dim pcd As PartComponentDefinition Set pcd = pt.ComponentDefinition Dim dpc As DerivedPartComponent For Each dpc In pcd.ReferenceComponents.DerivedPartComponents Dim rf As ReferenceFeature For Each rf In dpc.SolidBodies ' Get surface body of the original part ' this part is derived from Dim sb As SurfaceBody Set sb = rf.ReferencedEntity ' Now get the extrusion that created it Dim ef As ExtrudeFeature Set ef = sb.CreatedByFeature ' If either all the geometry comes from a ' sketch block or none of it then ' it's enough to check just the first entity ' in the profile path Dim pe As ProfileEntity Set pe = ef.Definition.Profile(1)(1) Dim skb As SketchBlock Set skb = pe.SketchEntity.ContainingSketchBlock If Not skb Is Nothing Then Call MsgBox(skb.Name) Else Call MsgBox("Not based on a sketch block") End If Next Next End Sub