[Approach Needed] - How to re-orient step files

[Approach Needed] - How to re-orient step files

aelqabbany
Advocate Advocate
527 Views
3 Replies
Message 1 of 4

[Approach Needed] - How to re-orient step files

aelqabbany
Advocate
Advocate

Hi,

 

I have a large number of step files that contain extruded parts which were drawn in random orientations. My objective is to get the extrusion direction to be in parallel to the Z-axis, then re-export those files as step files.

 

Not oriented.png

 

I can get the camera to face the "profile face" of the part through the code below, but I can't think of a way to set the XY plane to be parallel to the camera front.

 

Sub Main()
	oPartDoc = ThisApplication.ActiveDocument
	getLargestAreaFace(oPartDoc)
	
	oCamera = ThisApplication.ActiveView.Camera
	oCamera.ViewOrientationType = ViewOrientationTypeEnum.kRightViewOrientation
	oCamera.ApplyWithoutTransition
	ThisApplication.ActiveView.SetCurrentAsFront
End Sub


Function getLargestAreaFace(oPartDoc As PartDocument) As Face
    
    Dim maxarea As Double: maxarea = 0
    Dim oFace As Face
    Dim ofacemax As Face

    For Each oFace In oPartDoc.ComponentDefinition.SurfaceBodies.Item(1).Faces
        If oFace.SurfaceType <> kCylinderSurface Then
            If oFace.Evaluator.Area > maxarea Then
                maxarea = oFace.Evaluator.Area
                ofacemax = oFace
            End If
        End If
    Next
	
	Dim oSSet As SelectSet
	oSSet = oPartDoc.SelectSet
	Call oSSet.Clear
	Call oSSet.Select(ofacemax)
	
	ThisApplication.CommandManager.ControlDefinitions.Item("AppLookAtCmd").Execute2(True)
	ThisApplication.ActiveView.SetCurrentAsFront
End Function

 

The result:

trying to orient.png

The other approach I was thinking of is to create a new assembly, import the part, align the longest edge of the largest face to the Z-axis, then export the assembly as a step file.

 

I am open to any suggestions.

 

Thanks in advance.

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

Michael.Navara
Advisor
Advisor

Hi,

Bad news:

You are not able to redefine origin (base planes and axes).

You are not able to move existing body (without creating some transform features).

 

Good news:

You can copy existing body to the new position and hide the original body.

Then you can export STEP file with visible bodies only.

 

Here is code sample how to do it. It creates copy of selected body and sets the orientation. You can choose from three different implementations.

  • Fixed (precalculated) transformation
  • Transformation defined by active view
  • Transformation defined by planar face (face normal defines z axis) and linear edge (edge direction defines x axis)

Source body becomes invisible.

Final body is not moved to first quadrant and origin can be everywhere. If it is important for you, you need to modify the code.

 

 

 

Sub main
	Dim partDoc As PartDocument = ThisDoc.Document
	
	'Select body for transformation
	Dim srcBody As SurfaceBody = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartBodyFilter, "Select body")
	
	'Setup transformation
	'Select from following implementations
	Dim t As Matrix 
'	t = GetTransformationFixed()
'	t = GetTransformationFromActiveView()
	t = GetTransformationFromPlaneAndEdge()
	
	'Copy source body
	Dim tgtBody = ThisApplication.TransientBRep.Copy(srcBody)
	
	'Transform body to position
	ThisApplication.TransientBRep.Transform(tgtBody, t)

	Dim entities = ThisApplication.TransientObjects.CreateObjectCollection
	entities.Add(tgtBody)

	'Create NonParametricBaseFeatureDefinition
	Dim baseFeatureDef As NonParametricBaseFeatureDefinition = partDoc.ComponentDefinition.Features.NonParametricBaseFeatures.CreateDefinition()
	baseFeatureDef.BRepEntities = entities
	baseFeatureDef.OutputType = BaseFeatureOutputTypeEnum.kSolidOutputType

	'Create NonParametricBaseFeature
	partDoc.ComponentDefinition.Features.NonParametricBaseFeatures.AddByDefinition(baseFeatureDef)

	'Hide source body
	srcBody.Visible = False
	
	SetFinalCamera()
End Sub

Function GetTransformationFixed() As Matrix

	'Setup body transformation
	Dim tg = ThisApplication.TransientGeometry

	Dim o As Point = tg.CreatePoint(1, 2, 3)

	Dim x As Vector = tg.CreateVector(1, 1, 0)
	x.Normalize()

	Dim y As Vector = tg.CreateVector(-1, 1, 0)
	y.Normalize()

	Dim z As Vector = tg.CreateVector(0, 0, 1)
	z.Normalize()

	Dim t As Matrix = tg.CreateMatrix()
	t.SetCoordinateSystem(o, x, y, z)


	Return t
End Function



Function GetTransformationFromPlaneAndEdge() As Matrix

	Dim face As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Select top face")
	Dim plane As Plane = face.Geometry
	Dim n As UnitVector = plane.Normal

	Dim edge As Edge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeLinearFilter, "Select X edge")
	Dim line As LineSegment = edge.Geometry
	Dim d As UnitVector = line.Direction

	Dim o = face.PointOnFace
	Dim x = d.AsVector
	Dim z = n.AsVector
	Dim y = z.CrossProduct(x)
	y.Normalize

	Dim t As Matrix = ThisApplication.TransientGeometry.CreateMatrix()
	t.SetCoordinateSystem(o, x, y, z)
	t.Invert()
	
	Return t
End Function


Function GetTransformationFromActiveView() As Matrix
	Break
	Dim cam As Camera = ThisDoc.Document.Views(1).Camera
	Dim z = cam.Target.VectorTo(cam.Eye)
	z.Normalize

	Dim y = cam.UpVector.AsVector()

	Dim x = y.CrossProduct(z)
	x.Normalize

	Dim o = cam.Target
	o = ThisApplication.TransientGeometry.CreatePoint()

	Dim t As Matrix = ThisApplication.TransientGeometry.CreateMatrix()
	t.SetCoordinateSystem(o, x, y, z)
	t.Invert

	Return t
End Function

Sub SetFinalCamera
	Dim cam As Camera = ThisDoc.Document.Views(1).Camera
	Dim tg = ThisApplication.TransientGeometry
	cam.Target = tg.CreatePoint(0, 0, 0)
	cam.Eye = tg.CreatePoint(0, 0, 1)
	cam.UpVector = tg.CreateUnitVector(0, 1, 0)
	cam.ApplyWithoutTransition
End Sub

 

 

 

Message 3 of 4

Ralf_Krieg
Advisor
Advisor

Hello

 

I think you can not find the front face as largest face. This will work only in a few situations. I think you manually need to select the correct face.

Cause I've already wrote the code, i post it as alternative.

 

Sub Main()

    Dim oPartDoc As PartDocument= ThisApplication.ActiveDocument
    Dim oTrans As Transaction = ThisApplication.TransactionManager.StartTransaction(oPartDoc, "OrientSolid")

	Try
		'Dim oFaceMax As Face = getLargestAreaFace(oPartDoc)
		Dim oFaceMax As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter,"Select a face")
	    Dim oSSet As SelectSet= oPartDoc.SelectSet
	    
	    Call oSSet.Clear
	    Call oSSet.Select(oFaceMax)
	    
	    ThisApplication.CommandManager.ControlDefinitions.Item("AppLookAtCmd").Execute2 (True)
	    ThisApplication.ActiveView.SetCurrentAsFront
	    
	    Call oSSet.Clear
	    Call OrientSolid(oPartDoc)
	    
	    Dim oWorkPlane As WorkPlane
	    For Each oWorkPlane In oPartDoc.ComponentDefinition.WorkPlanes
	        If Left(oWorkPlane.Name, 2) = "XY" Then Exit For
	    Next
	    
	    Call oSSet.Select(oWorkPlane)
	    
	    ThisApplication.CommandManager.ControlDefinitions.Item("AppLookAtCmd").Execute2 (True)
	    ThisApplication.ActiveView.SetCurrentAsFront
	    ThisApplication.ActiveView.Fit
	Catch
		oTrans.Abort
	Finally
		oTrans.End	
	End Try
End Sub

Private Sub OrientSolid(ByVal oPartDoc As PartDocument)
	Dim oApp As Inventor.Application = ThisApplication
	Dim oSB As SurfaceBody = oPartDoc.ComponentDefinition.SurfaceBodies(1)
	Dim oTransBRep As TransientBRep = oApp.TransientBRep
	Dim oTransUnion As SurfaceBody= oTransBRep.Copy(oSB)

	Dim oAddBody As SurfaceBody
	Dim oTransAdd As SurfaceBody

	If oPartDoc.ComponentDefinition.SurfaceBodies.Count > 1 Then
	    Dim i As Integer
	    For i = 2 To oPartDoc.ComponentDefinition.SurfaceBodies.Count
	        oAddBody = oPartDoc.ComponentDefinition.SurfaceBodies(i)
	        oTransAdd = oTransBRep.Copy(oAddBody)
	        Call oTransBRep.DoBoolean(oTransUnion, oTransAdd, kBooleanTypeUnion)
	    Next
	End If

	Dim oCamera As Camera= ThisApplication.ActiveView.Camera
	Dim oMatrix As Matrix = oCamera.WorldToView

	Call oTransBRep.Transform(oTransUnion, oMatrix)

	Dim NonParaDef As NonParametricBaseFeatureDefinition= oPartDoc.ComponentDefinition.Features.NonParametricBaseFeatures.CreateDefinition
	Dim oBodyColl As ObjectCollection = oApp.TransientObjects.CreateObjectCollection

	Call oBodyColl.Add(oTransUnion)

	NonParaDef.BRepEntities = oBodyColl
	NonParaDef.OutputType = BaseFeatureOutputTypeEnum.kSolidOutputType
	NonParaDef.DeleteOriginal=True

	Dim NonParamBaseFeat As NonParametricBaseFeature= oPartDoc.ComponentDefinition.Features.NonParametricBaseFeatures.AddByDefinition(NonParaDef)
	
	NonParamBaseFeat.Name = "Rotated_Solid"
	
	For Each oFeature As Object In oPartDoc.ComponentDefinition.Features
	    If Not oFeature.name = "Rotated_Solid" Then oFeature.delete
	Next

End Sub

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 4 of 4

aelqabbany
Advocate
Advocate

Thank you both very much. I will test both approaches next week, God willing.

0 Likes