Hello Curtis,
Here's the code where you put out one preparation from a weldment on the drawing and then my code tries to rotate them correctly. I see that I both need help with the rotation of the part depending on the flatpattern orientation but also if the part is not modelled in x y z ( for example 45 degrees in to x plane) I have begun to work on it and think the oriented box measure of the part can be used to find the largest box measure on the largest side where I have my face normal and rotate it down to the horizontal vector.
Sub main()
'On Error Resume Next
Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim oSheet As Sheet = oDoc.ActiveSheet
Dim doc As AssemblyDocument = ThisDrawing.ModelDocument
Dim Words As New ArrayList
Dim Separators() As Char = {":"}
Dim drawingViewInSheet As DrawingView
Dim weldObject As WeldmentStateEnum
Dim comp As Object
Dim oScale = oSheet.DrawingViews.Item(1).Scale
Dim oXDim As Long
Dim oYDim As Long
Dim oZDim As Long
Dim oModelWidth As Long
Dim oModelHeight As Long
Dim i As Integer
For Each drawingViewInSheet In oSheet.DrawingViews
Try
oViewModelDoc = drawingViewInSheet.ReferencedDocumentDescriptor.ReferencedDocument
If oViewModelDoc.SubType = "{28EC8354-9024-440F-A8A2-0E0E55D635B0}"
Call drawingViewInSheet.GetWeldmentState(weldObject, comp)
If weldObject.ToString = "kPreparationsWeldmentState"
If comp.Type = kComponentOccurrenceObject Then
fullString = comp.Name.Split(Separators)
Dim onlyName As String = fullString(0)
'MsgBox(onlyName)
Words.Add(onlyName)
End If
End If
End If
Catch
End Try
Next
''availableScales = New String(){"1 : 1", "1 : 2", "1 : 5", "1 : 10", "1 : 20"}
''selectedScale = InputListBox("Choose scale", availableScales, "", Title := "Scale", ListName := "Available scales")
'Dim oScale As Decimal
'If selectedScale = "1 : 1"
' oScale = 1
'Else If selectedScale = "1 : 2"
' oScale = 0.5
'Else If selectedScale = "1 : 5"
' oScale = 0.2
'Else If selectedScale = "1 : 10"
' oScale = 0.1
'Else If selectedScale = "1 : 20"
' oScale = 0.05
'End If
Dim oOccName As String
Dim oOcc As ComponentOccurrence
Dim NewWords As New ArrayList
Dim j
Dim oPT2d As Point2d
Dim oView2pointX
For Each oOcc In doc.ComponentDefinition.Occurrences
'oFullString = oPartNumber = oDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
If oOcc.Suppressed = False Then
oFullString = oOcc.Name.Split(Separators)
oOccName= oFullString(0)
j = 0
Dim wordString As String
For Each wordString In Words
If wordString = oOccName
j = j + 1
End If
Next
Dim PropertySet = oOcc.Definition.Document.PropertySets.Item("Inventor User Defined Properties")
If oOcc.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject And j = 0 Then
If LCase(oOcc.Name).Contains("skeleton")=True Or LCase(oOcc.Name).Contains("frame reference")=True Or LCase(oOcc.Name).Contains(".iam")=True Then'Or LCase(PropertySet.item("0_Type").Value) = "fastener" Then
Else
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
oSheet = oDoc.ActiveSheet
Dim oPartslist As PartsList
Dim oDrawingView As DrawingView
oDrawingView = oSheet.DrawingViews(1)
Dim oPlacementPoint, oPlacementPoint2 As Point2d
Dim oBorder As Border
Dim PartListHeight As Integer
Dim TitleY As Double
Dim PointX
Dim PointY
oBorder = oSheet.Border
Dim oPartPath = oOcc.ReferencedDocumentDescriptor.FullDocumentName
Dim oviewDoc
oviewDoc = ThisApplication.Documents.Open(oPartPath,False)
Dim oPart= ThisApplication.Documents.ItemByName(oPartPath)
Dim oPDoc = ThisApplication.ActiveDocument
Dim oPDef = oPart.ComponentDefinition
RuleParametersOutput()
InventorVb.DocumentUpdate()
Dim oRangeBox As Box = oPDef.RangeBox
oXDim = (oRangeBox.MaxPoint.X - oRangeBox.MinPoint.X)
oYDim = (oRangeBox.MaxPoint.Y - oRangeBox.MinPoint.Y)
oZDim= (oRangeBox.MaxPoint.Z - oRangeBox.MinPoint.Z)
Dim HasBend As Integer
HasBend = 0
Dim dX As Double = oRangeBox.MaxPoint.X
Dim dY As Double = oRangeBox.MaxPoint.Y
oModelWidth = MaxOfMany(oXDim, oYDim, oZDim)
oModelHeight = MinOfMany(oXDim, oYDim, oZDim)
If oPart.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
Dim oSMDef As SheetMetalComponentDefinition = oPart.ComponentDefinition
If oSMDef.HasFlatPattern = True Then
If oSMDef.FlatPattern.FlatBendResults.Count > 0 Then
HasBend = 1
End If
End If
End If
Dim oface As Face
oface = getLargestAreaFace(oviewDoc.ComponentDefinition)
'Call SetCamera(oPart,oface.Geometry.Normal)
Dim LongestEdge As Edge
LongestEdge = GetLongestEdge(oviewDoc.ComponentDefinition)
Dim oViewModel As Inventor.View = oviewDoc.Views.Add
Dim otg As TransientGeometry
otg = ThisApplication.TransientGeometry
Dim ocam As Camera
ocam = oViewModel.Camera
Dim oVector As UnitVector
oVector = oface.Geometry.normal
ocam.Target = otg.CreatePoint(oVector.X, oVector.Y, oVector.Z)
ocam.Eye = otg.CreatePoint(0, 0, 0)
Dim oZAxis As UnitVector
Dim oYAxis As UnitVector
Dim oXAxis As UnitVector
oZAxis = otg.CreateUnitVector(0, 0, 1)
oYAxis = otg.CreateUnitVector(0, 1, 0)
oXAxis = otg.CreateUnitVector(1, 0, 0)
Dim LongestedgeX As Integer
Dim LongestedgeY As Integer
Dim LongestedgeZ As Integer
LongestedgeX = LongestEdge.StopVertex.Point.X - LongestEdge.StartVertex.Point.X
LongestedgeY = LongestEdge.StopVertex.Point.Y - LongestEdge.StartVertex.Point.Y
LongestedgeZ = LongestEdge.StopVertex.Point.Z - LongestEdge.StartVertex.Point.Z
'Get range box center point
Dim centerPoint As Point
centerPoint = GetRangeBoxCenter(oviewDoc)
Dim PI As Double
PI = Math.PI
Dim LongestedgeVector As UnitVector
LongestedgeVector = LongestEdge.StartVertex.Point.VectorTo(LongestEdge.StopVertex.Point).AsUnitVector
If oVector.IsParallelTo(oZAxis, 0.000001) Then
'MsgBox("X: " & LongestedgeX & "; Y: " & LongestedgeY)
If Abs(LongestedgeX) > Abs(LongestedgeY) Then
ocam.UpVector = oYAxis
Else
'MsgBox("face_vector_Z_up_X")
ocam.UpVector = oXAxis
End If
Else If oVector.IsParallelTo(oXAxis, 0.000001) Then
If Abs(LongestedgeZ) > Abs(LongestedgeY) Then
ocam.UpVector = oYAxis
'MsgBox("face_vector_X_up_Y")
Else
ocam.UpVector = oZAxis
End If
Else If oVector.IsParallelTo(oYAxis, 0.000001)
'MsgBox("Z: " & LongestedgeZ & "; X: " & LongestedgeX)
If Abs(LongestedgeZ) > Abs(LongestedgeX) Then
' MsgBox("face_vector_Y_up_X")
ocam.UpVector = oXAxis
If LongestedgeZ<0 Then
'MsgBox("Z<0")
'RotateCam(ocam,PI,oXAxis,centerPoint)
End If
Else
ocam.UpVector = oZAxis
' MsgBox("face_vector_Y_up_Z")
If LongestedgeX<0 Then
' MsgBox("X<0")
'RotateCam(ocam,PI,oZAxis,centerPoint)
End If
End If
End If
ocam.Fit
ocam.Apply
oPart.Close
Dim oPT2dSide As Point2d
Dim PositionYInt As Integer
If j = 1 Then
oPT2d = ThisApplication.TransientGeometry.CreatePoint2d(-50, 80) ' to change the placement with coordinates
Else
oPT2d = ThisApplication.TransientGeometry.CreatePoint2d(i, 80)
End If
_doc = ThisDoc.Document
_sheet = _doc.ActiveSheet
_intents = New List(Of GeometryIntent)()
Dim oView As DrawingView
oView = oSheet.DrawingViews.AddBaseView(doc, oPT2d, oScale, ViewOrientationTypeEnum.kArbitraryViewOrientation , DrawingViewStyleEnum.kHiddenLineDrawingViewStyle,,ocam)
oView.SetWeldmentState(WeldmentStateEnum.kPreparationsWeldmentState, oOcc)
oView.DisplayTangentEdges = True
Dim prt
'prt = oView.ReferencedDocumentDescriptor.ReferencedDocument
Dim oViewPosition As Point2d
' set position of views
PositionYInt = oSheet.Height + 8
'oView.Rotation = oViewRotation
oView.ShowLabel = True
oView.Label.Position = ThisApplication.TransientGeometry.CreatePoint2d(oView.Position.X,oView.Position.Y + (oView.Height/2) + 3)
If Not j = 1 Then
oView2pointX = Round(oView.Position.X + (oView.Width/2) + 6,0)
oView.Position = ThisApplication.TransientGeometry.CreatePoint2d(oView2pointX, PositionYInt)
End If
'oViewPosi
i = oView.Position.X + (oView.Width / 2) + 2
If HasBend = 1 Then
'Projection_Under = oSheet.DrawingViews.AddProjectedView(oView, ThisApplication.TransientGeometry.CreatePoint2d(oView.Position.X, proj_under_intY ) , DrawingViewStyleEnum.kFromBaseDrawingViewStyle)
Dim oPT2dFlatpattern As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oView.Position.X,oView.Position.Y + oView.Height)
Dim FlattpatternView As DrawingView = oSheet.DrawingViews.AddBaseView(oviewDoc,oPT2dFlatpattern,oScale,ViewOrientationTypeEnum.kDefaultViewOrientation , DrawingViewStyleEnum.kHiddenLineDrawingViewStyle,,, oBaseViewOptions)
FlattpatternView.Label.FormattedText = "PLÅTUTBREDNINGSEXEMPEL" & vbNewLine & "<StyleOverride> ( <DrawingViewScale/> )</StyleOverride>"
FlattpatternView.Label.HorizontalJustification = HorizontalTextAlignmentEnum.kAlignTextCenter
FlattpatternView.ShowLabel = True
Dim oView2pointY
oView2pointY = FlattpatternView.Position.Y + (FlattpatternView.Height / 2) + 5
FlattpatternView.Position = ThisApplication.TransientGeometry.CreatePoint2d(oView.Position.X, oView2pointY)
end if
End If
End If
End If
Next
End Sub
Public Function getLargestAreaFace(compdef As ComponentDefinition) As Face
Dim maxarea As Double
maxarea = 0
Dim oface As Face
Dim ofacemax As Face
For Each oface In compdef.SurfaceBodies.Item(1).Faces
If Not oface.SurfaceType = kCylinderSurface Then
If oface.Evaluator.Area > maxarea Then
maxarea = oface.Evaluator.Area
ofacemax = oface
End If
End If
Next
getLargestAreaFace = ofacemax
End Function
Function GetLongestEdge(compdef As ComponentDefinition) As Edge
'Dim oEdges As Edges
Dim oEdge As Edge
Dim maxLength As Double = 0
Dim longestEdge As Edge
Dim Edgelength
For Each oEdge In compdef.SurfaceBodies.Item(1).Edges
Edgelength = oEdge.StartVertex.Point.VectorTo(oEdge.StopVertex.Point).Length
If Edgelength > maxLength Then
maxLength = Edgelength
longestEdge = oEdge
End If
Next
Return longestEdge
End Function
Sub SetCamera(oMDoc As Document, oVector As UnitVector)
Dim oViewModel As Inventor.View = oMDoc.Views.Add
Dim otg As TransientGeometry
otg = ThisApplication.TransientGeometry
Dim ocam As Camera
ocam = oViewModel.Camera
ocam.Target = otg.CreatePoint(oVector.X, oVector.Y, oVector.Z)
ocam.Eye = otg.CreatePoint(0, 0, 0)
Dim oZAxis As UnitVector
oZAxis = otg.CreateUnitVector(0, 0, 1)
If oVector.IsParallelTo(oZAxis, 0.000001) Then
ocam.UpVector = otg.CreateUnitVector(0, 1, 0)
Else
Dim oTemp As UnitVector
oTemp = oVector.CrossProduct(oZAxis)
Dim oUp As UnitVector
oUp = oTemp.CrossProduct(oVector)
ocam.UpVector = oUp
End If
'Dim _ViewOrientation As ViewOrientationTypeEnum = ViewOrientationTypeEnum.kFrontViewOrientation
'restore settings
'ocam.ViewOrientationType = _ViewOrientation
ocam.fit
ocam.Apply
End Sub
Public Function GetRangeBoxCenter(ByVal doc As Document) As Point
Dim minPoint As Point
minPoint = doc.ComponentDefinition.RangeBox.minPoint
Dim maxPoint As Point
maxPoint = doc.ComponentDefinition.RangeBox.maxPoint
GetRangeBoxCenter = ThisApplication.TransientGeometry.CreatePoint( _
(minPoint.X + maxPoint.X) * 0.5, _
(minPoint.Y + maxPoint.Y) * 0.5, _
(minPoint.Z + maxPoint.Z) * 0.5)
End Function