How to Rotate camera in view before adding it to baseview so it be the same orientation as flatpattern baseview?

How to Rotate camera in view before adding it to baseview so it be the same orientation as flatpattern baseview?

andreas.jonssonF98YC
Enthusiast Enthusiast
247 Views
3 Replies
Message 1 of 4

How to Rotate camera in view before adding it to baseview so it be the same orientation as flatpattern baseview?

andreas.jonssonF98YC
Enthusiast
Enthusiast

Hi I struggle with the orientation of the camera view so it orients correctly to a baseview. for plates it should be orientated in the same way as flatpattern view but since no axis or orientation of the part itself can be modified I would like to rotate the camera correct undependent on which orientation the part have in space. I have tried with both largest face and defining a A side but it have not worked.  underneath is the result right now. The flatpattern bendnote says down and my part shows that the bend is upwards. even if I have put a negative normal on the surface at the camera.eye it looks on the correct surface but the z up surface shall be down 

 

andreasjonssonF98YC_0-1732792494817.png

 

correct answer shall be x out z down and Y left for this. 

andreasjonssonF98YC_1-1732792868339.png

 

 

can you help me with the orientation of the camera for all directions of parts?

 

 

If oPart.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
	oSMDef = 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)
'Tried with Aside 'If oSMDef.ASideFaceStatus = kASideUpToDate Then 'MsgBox(oSMDef.ASideDefinitions.Count) 'oface = oSMDef.ASideDefinitions.Item(1).ASideFace 'Next 'oface = oSMDef.ASideDefinitions.Item() '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
'first made this vector. Then camera look at surface from wrong direction but bend is on the correct side.
ocam
.Target = otg.CreatePoint(oVector.X, oVector.Y, oVector.Z)
'second try looks at the part from correct side but since i put z as upvector it orientates the bend to the right side 'ocam.Target = otg.CreatePoint(-oVector.X, -oVector.Y, -oVector.Z) ocam.Eye = otg.CreatePoint(0, 0, 0)

'This is also incorrect if part is rotated in diagonal of x y Z so that I need to fix, any ideas? 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 ' define 180 degrees as radiant double ' Dim dRad180 As Double ' dRad180 = 4 * Math.Atan(1) 'Dim oMatrix As Matrix 'oMatrix = ocam.viewtViewToWorld 'looks at which axis that are parallel to largest face and then check which of the others that are the longest to have on the
'horizontal side Dim LongestedgeVector As UnitVector LongestedgeVector = LongestEdge.StartVertex.Point.VectorTo(LongestEdge.StopVertex.Point).AsUnitVector If oVector.IsParallelTo(oZAxis, 0.000001) Then If Abs(LongestedgeX) > Abs(LongestedgeY) Then ocam.UpVector = oYAxis If LongestedgeX<0 Then 'RotateCam(ocam,PI,oYAxis,centerPoint) End If Else ocam.UpVector = oXAxis If LongestedgeY<0 Then 'RotateCam(ocam, PI, oXAxis, centerPoint) End If End If Else If oVector.IsParallelTo(oXAxis, 0.000001) Then If Abs(LongestedgeZ) > Abs(LongestedgeY) Then ocam.UpVector = oYAxis If LongestedgeZ<0 Then 'RotateCam(ocam,PI,oYAxis,centerPoint) End If Else 'msgbox("Hej") ocam.UpVector = oZAxis If LongestedgeY<0 Then 'RotateCam(ocam,PI,oZAxis,centerPoint) End If End If Else If oVector.IsParallelTo(oYAxis, 0.000001) If Abs(LongestedgeZ) > Abs(LongestedgeX) Then ocam.UpVector = oXAxis If LongestedgeZ<0 Then 'RotateCam(ocam,PI,oXAxis,centerPoint) End If Else ocam.UpVector = oZAxis If LongestedgeX<0 Then 'RotateCam(ocam,PI,oZAxis,centerPoint) End If End If End If ocam.Fit ocam.Apply oPart.Close


thank you in advance

 

 

0 Likes
248 Views
3 Replies
Replies (3)
Message 2 of 4

andreas.jonssonF98YC
Enthusiast
Enthusiast

@Curtis_Waguespack @johnsonshiue @JelteDeJong   Maybe you have any idea on how to make this work?

0 Likes
Message 3 of 4

Curtis_Waguespack
Consultant
Consultant

Your example seems to be expecting some functions that you did not provide.

 

Error on Line 16 : 'getLargestAreaFace' is not declared. It may be inaccessible due to its protection level.
Error on Line 26 : 'GetLongestEdge' is not declared. It may be inaccessible due to its protection level.
Error on Line 62 : 'GetRangeBoxCenter' is not declared. It may be inaccessible due to its protection level.



EESignature

0 Likes
Message 4 of 4

andreas.jonssonF98YC
Enthusiast
Enthusiast

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


 

0 Likes