Help! I am trying to project geometry (a floor plan) onto a DrawingSketch using the AddByProjectingEntity method. The active document is a Drawing; I have created the DrawingSketch from the active view. Yet nothing I try seems to work. Can it really be that difficult? A sample of one of my attempts follows. What am I missing?
'Get the drawing document.
Dim thisDwg As DrawingDocument = InvApp.ActiveDocument
'Get the sheet.
Dim thisSheet As Sheet = thisDwg.ActiveSheet
'Get the active view.
Dim thisView As DrawingView = thisSheet.DrawingViews.Item(1)
'Create a sketch.
Dim thisDwgSketch As DrawingSketch = thisView.Sketches.Add
'Start editing the sketch.
thisDwgSketch.Edit()
'Get the referenced assembly document.
Dim AssyDoc As AssemblyDocument = thisView.ReferencedDocumentDescriptor.ReferencedDocument
Dim AssyCompDef As AssemblyComponentDefinition = AssyDoc.ComponentDefinition
Dim Backstop1 As ComponentOccurrence = AssyCompDef.Occurrences.Item(1)
Dim Gym1 As ComponentOccurrence = Nothing
For Each Gym1 In Backstop1.Definition.Occurrences
If InStr(Gym1.Name, "GYM") Then
Exit For
End If
Next
Dim oOccur As ComponentOccurrence = Nothing
For Each oOccur In Gym1.Definition.Occurrences
If InStr(oOccur.Name, "WALLS") Then
For Each SurfBody As SurfaceBody In oOccur.SurfaceBodies
For Each oEdge As Edge In SurfBody.Edges
Dim anObject As Object = Nothing
oOccur.CreateGeometryProxy(oEdge, anObject)
thisDwgSketch.AddByProjectingEntity(anObject)
Next
Next
Exit For
End If
Next
thisDwgSketch.ExitEdit()
Solved! Go to Solution.
Solved by xiaodong_liang. Go to Solution.
When using the AddByProjectingEntity method in a drawing you need to provide an entity that exists in the context of a drawing. You were getting the edge in the context of the assembly. The object that represents an edge in the drawing is a DrawingCurve object. I converted your program to VBA because it's quicker to test and modified it to work. In my test I had a simple assembly with two parts where one of them is named "BoxInPocket". It looks for this part and then projects all of its edges onto the newly created sketch.
Public Sub ProjectEntity() 'Get the drawing document. Dim thisDwg As DrawingDocument Set thisDwg = ThisApplication.ActiveDocument 'Get the sheet. Dim thisSheet As Sheet Set thisSheet = thisDwg.ActiveSheet 'Get the active view. Dim thisView As DrawingView Set thisView = ThisApplication.CommandManager.Pick(kDrawingViewFilter, _ "Select the drawing view.") 'Create a sketch on the view. Dim thisDwgSketch As DrawingSketch Set thisDwgSketch = thisView.Sketches.Add ' Get the referenced assembly. Dim asmDoc As AssemblyDocument Set asmDoc = thisView.ReferencedDocumentDescriptor.ReferencedDocument ' Get the occurrence named "BoxInPocket" Dim boxOcc As ComponentOccurrence For Each boxOcc In asmDoc.ComponentDefinition.Occurrences If InStr(boxOcc.Name, "BoxInPocket") Then ' Iterate over the the edges in the part. ' Since the surface body is obtained from the occurence ' it is a SurfaceBodyProxy and will return proxy edges ' in the context of the top-level assembly. Dim partEdge As Edge For Each partEdge In boxOcc.SurfaceBodies.Item(1).Edges ' Attempt to get any drawing curves associated with this edge. ' This will return curves if the edge is visible in the view. Dim drawCurves As DrawingCurvesEnumerator Set drawCurves = thisView.DrawingCurves(partEdge) ' Project each drawing curve onto the sketch. Dim drawCurve As DrawingCurve For Each drawCurve In drawCurves Dim ent As SketchEntity Set ent = thisDwgSketch.AddByProjectingEntity(drawCurve) Next Next End If Next End Sub
Thanks Brian,
I got your version to work fine, both in VBA and Vb.net. Mine, however, is still telling me that my parameter is incorrect when it gets to this line:
Dim drawCurves As DrawingCurvesEnumerator = thisView.DrawingCurves(oEdge)
The only difference that I can tell is that the part I'm wanting to project is a few levels deep into the assembly, whereas yours is a top-level part. Could that be making a difference?
Hi Larry,
yes, you will have to recurse the sub assembly to get the part. The following code is for you reference.
Public Sub ProjectEntity() 'Get the drawing document. Dim thisDwg As DrawingDocument Set thisDwg = ThisApplication.ActiveDocument 'Get the sheet. Dim thisSheet As Sheet Set thisSheet = thisDwg.ActiveSheet 'Get the active view. Dim thisView As DrawingView Set thisView = ThisApplication.CommandManager.Pick(kDrawingViewFilter, _ "Select the drawing view.") 'Create a sketch on the view. Dim thisDwgSketch As DrawingSketch Set thisDwgSketch = thisView.Sketches.Add ' Get the referenced assembly. Dim asmDoc As AssemblyDocument Set asmDoc = thisView.ReferencedDocumentDescriptor.ReferencedDocument ' Get the occurrence named "BoxInPocket" Dim boxOcc As ComponentOccurrence For Each boxOcc In asmDoc.ComponentDefinition.Occurrences 'For Each boxOcc In asmDoc.ComponentDefinition.Occurrences If InStr(boxOcc.Name, "Assembly1:1") Then ' Iterate over the the edges in the part. ' Since the surface body is obtained from the occurence ' it is a SurfaceBodyProxy and will return proxy edges ' in the context of the top-level assembly. ' Call recurse_occ(boxOcc, thisView, thisDwgSketch) End If Next End Sub Sub recurse_occ(oParentOcc As ComponentOccurrence, thisView As DrawingView, thisDwgSketch As DrawingSketch) If TypeOf oParentOcc.Definition Is PartComponentDefinition Then Dim partEdge As Edge For Each partEdge In oParentOcc.SurfaceBodies.Item(1).Edges ' Attempt to get any drawing curves associated with this edge. ' This will return curves if the edge is visible in the view. Dim drawCurves As DrawingCurvesEnumerator Set drawCurves = thisView.DrawingCurves(partEdge) ' Project each drawing curve onto the sketch. Dim drawCurve As DrawingCurve For Each drawCurve In drawCurves Dim ent As SketchEntity Set ent = thisDwgSketch.AddByProjectingEntity(drawCurve) Next Next Else ' if this occurrence is a sub assembly Dim oSubOcc As ComponentOccurrence For Each oSubOcc In oParentOcc.SubOccurrences Call recurse_occ(oSubOcc, thisView, thisDwgSketch) Next End If End Sub
You can also get all referenced documents of the top assembly by AllReferencedDocuments, check which are part, and add the project sketch (similar code above) for them in the drawing.
Thanks Xiaodong,
I finally got it to work, but not as I expected.
Initially, I searched through the top level assembly to find a particular backstop, and then searched through the backstop to find its gym, and then searched through the gym to find the walls. This, however, was resulting in an "incorrect parameter" error.
I then modified your method to recursively search, starting at the top, and continue through everything until I arrived at a part with the name I was looking for. Doing it this way worked. Not sure why, but at least I can continue on now.
Thanks again.