I've created a function to create a linear dimension on two workplanes. I made this so that it looks in the referenced document and then gets the occurrence with a specific name. This works fine when I have an assembly on the drawing and have the work planes in a component, but sometimes I have a part on the drawing. I'd like to be able to use the same function to dimension this part by leaving the argument for the occurrence name empty. Can anyone suggest how to do this without either writing two separate functions or writing one function with a lot of repeated code for the separate cases?
I have attached an example containing a rule that works for the assembly view, but at the moment not the part view.
Here is the same code as that rule:
Sub Main
' Set a reference to the Drawing Document
Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveEditDocument
' Set a reference to the Sheets
Dim oSheets As Sheets = oDrawDoc.Sheets
' Set a reference to the Drawing Sheet
Dim oSheet As Sheet = oDrawDoc.ActiveSheet
' Set a reference to the Drawing Views
Dim oDrawingViews As DrawingViews = oSheet.DrawingViews
' Set a reference to the Drawing Views
Dim sDrawingViewNameAsm As String = "ViewAssembly"
Dim sDrawingViewNamePart As String = "ViewPart"
Dim oDrawingViewAsm As DrawingView
Dim oDrawingViewPart As DrawingView
For i As Integer = 1 To oDrawingViews.Count
If oDrawingViews.Item(i).Name = sDrawingViewNameAsm Then oDrawingViewAsm = oDrawingViews.Item(i)
If oDrawingViews.Item(i).Name = sDrawingViewNamePart Then oDrawingViewPart = oDrawingViews.Item(i)
Next
' Create Dimensions
CreateLinearDimensionHorizontal(oSheet, oDrawingViewAsm, "Bottom", 1, "WPL_0_Breedte", "WPL_100_Breedte", "Test")
CreateLinearDimensionHorizontal(oSheet, oDrawingViewAsm, "Bottom", 2, "WPL_0_Breedte", "WPL_200_Breedte", "Test")
End Sub
Public Function CreateLinearDimensionHorizontal(ByVal oSheet As Sheet, ByVal oDrawingView As DrawingView, ByVal sDimensionSide As String, ByVal dDimensionLevel As Double, ByVal sWorkPlaneName1 As String, ByVal sWorkPlaneName2 As String, ByVal sOccName As String) As LinearGeneralDimension
' Set references to documents, and component definitions
Dim oModelAsm As AssemblyDocument = oDrawingView.ReferencedDocumentDescriptor.ReferencedDocument
Dim oModelAsmCompDef As AssemblyComponentDefinition = oModelAsm.ComponentDefinition
Dim oModelPart As ComponentOccurrence = oModelAsmCompDef.Occurrences.ItemByName(sOccName)
Dim oModelPartDoc As PartDocument = oModelPart.Definition.Document
Dim oModelPartCompDef As PartComponentDefinition = oModelPartDoc.ComponentDefinition
' Set references to workplanes
Dim oWPlane1 As WorkPlane = oModelPartCompDef.WorkPlanes.Item(sWorkPlaneName1)
Dim oWPlane2 As WorkPlane = oModelPartCompDef.WorkPlanes.Item(sWorkPlaneName2)
' Create proxies for workplanes
Dim oWPlane1Proxy As WorkPlaneProxy
oModelPart.CreateGeometryProxy(oWPlane1, oWPlane1Proxy)
Dim oWPlane2Proxy As WorkPlaneProxy
oModelPart.CreateGeometryProxy(oWPlane2, oWPlane2Proxy)
' Include workplanes and turn visibility off
oDrawingView.SetIncludeStatus(oWPlane1Proxy, True)
oDrawingView.SetVisibility(oWPlane1Proxy, False)
oDrawingView.SetIncludeStatus(oWPlane2Proxy, True)
oDrawingView.SetVisibility(oWPlane2Proxy, False)
' Create geometry intents for workplanes
Dim oWPlane1Intent As GeometryIntent
Dim oWPlane2Intent As GeometryIntent
Dim oCenterline As Centerline
For Each oCenterline In oSheet.Centerlines
If oCenterline.ModelWorkFeature Is oWPlane1Proxy Then
oWPlane1Intent = oSheet.CreateGeometryIntent(oCenterline)
ElseIf oCenterline.ModelWorkFeature Is oWPlane2Proxy Then
oWPlane2Intent = oSheet.CreateGeometryIntent(oCenterline)
End If
Next
' Set spacing between dimensions
Dim oDimSpacing As Double = 1
' Get positions of view extents
Dim oDrawingViewBottom As Double = oDrawingView.Top - oDrawingView.Height
Dim oDrawingViewTop As Double = oDrawingView.Top
' Set level of dimension
Dim oDimensionLevel As Double = dDimensionLevel
' Set dimension position
Dim oDrawingViewDimPosition As Double
If sDimensionSide = "Bottom" Then
oDrawingViewDimPosition = oDrawingViewBottom
ElseIf sDimensionSide = "Top" Then
oDrawingViewDimPosition = oDrawingViewTop
End If
' Create transient point
Dim oPointY As Double
If sDimensionSide = "Bottom" Then
oPointY = oDrawingViewDimPosition - oDimensionLevel * oDimSpacing
ElseIf sDimensionSide = "Top" Then
oPointY = oDrawingViewDimPosition + oDimensionLevel * oDimSpacing
End If
Dim oPointX As Double = 0
Dim oPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oPointX, oPointY)
' Create linear dimension
Dim oGeneralDims = oSheet.DrawingDimensions.GeneralDimensions
Dim oLinearDim = oGeneralDims.AddLinear(oPoint, oWPlane1Intent, oWPlane2Intent)
oLinearDim.CenterText
' Return the result
Return oLinearDim
End Function
Can't find what you're looking for? Ask the community or share your knowledge.