Message 1 of 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi All,
I am trying to develop this code but have come across a hurdle.
The goal is to
access the referenced parts in an assembly,
filter using part name (P is used for this, filename format is: part-P001.ipt),
get the solidbody orientation,
set view to match the solidbody,
apply the changes to the camera/view,
set current view as front for the part file.
I can get this to work if i use the code to open each part, set the view then save and close.
I would prefer it if I didn't need to open each file.
The code below seems to work for all but set current view to front.
Any help would be welcome, Thanks.
Roy
Doc As AssemblyDocument = ThisApplication.ActiveDocument
For Each refDoc As Document In oDoc.AllReferencedDocuments
' Check if the document is a part (we only want to work with parts)
If refDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
Select Left(Right(refDoc.DisplayName, 8), 1)
Case "P"
' Process the part document
Dim partDoc As PartDocument = refDoc
' Get the component definition of the part
Dim compDef As PartComponentDefinition = partDoc.ComponentDefinition ' Ensure the part has a solid body
Dim solidBody As SurfaceBody = compDef.SurfaceBodies.Item(1)
Dim omrbBox As OrientedBox = solidBody.OrientedMinimumRangeBox
' Get the length of the OrientedBox along each direction
Dim xDim As Double = omrbBox.DirectionOne.Length * 10 ' mm
Dim yDim As Double = omrbBox.DirectionTwo.Length * 10 ' mm
Dim zDim As Double = omrbBox.DirectionThree.Length * 10 ' mm
' View Section
' Get the directions and their lengths
Dim dirX As Vector = omrbBox.DirectionOne
Dim dirY As Vector = omrbBox.DirectionTwo
Dim dirZ As Vector = omrbBox.DirectionThree
' Create an array of directions with their corresponding lengths
Dim vectorData As (Vector, Double)() = {
(dirX, xDim),
(dirY, yDim),
(dirZ, zDim)
}
' Sort vectors by their lengths (shortest to longest)
Dim sortedVectors = vectorData.OrderBy(Function(v) v.Item2).ToArray()
' The shortest vector is the first in the sorted array
Dim zVector As Vector = sortedVectors(0).Item1
' The longest vector is the last in the sorted array
Dim yVector As Vector = sortedVectors(1).Item1
Dim oCam As Camera = ThisApplication.ActiveView.Camera
Dim Eyeposition As Point = omrbBox.CornerPoint.Copy() : Eyeposition.TranslateBy(zVector)
' ' Set camera properties
With oCam
.Target = omrbBox.CornerPoint
.Eye = Eyeposition
.UpVector = yVector.AsUnitVector ' Ensure UpVector is normalized
End With
' Set this view as the "Front" view
oCam.Fit
' oCam.ApplyWithoutTransition
oCam.Apply()
Dim oView As View = ThisApplication.ActiveView
oView.SetCurrentAsFront
' oCam.Parent.SetCurrentAsFront
' oCam.Parent.ResetFront 'resets the front orientation
End Select
End If
Next
Solved! Go to Solution.