auto rotate view based on max dimension of flat c#

I have a macro that auto generate flats, add values from custom properties and auto dimension of flat for manufacturing process. On flat view the dimension are automated I can retrieve the dimensions list from this view, but I want to rotate it if dimension on Y is bigger than X.

1) Do we have a function or method for specific dimensions placed horizontal or vertical to use it later as flag for rotation by comparing values.

2) Another hypothesis is to retrieve the view dimensions and rotate it ??

I hope I made my self clear.

Thank you

SolidworksApi macros
Answers 5
NP
NP 2017-11-04 Best Answer
AL
AL 2017-11-06

Thank you Patel for example. Your reply is correct but not sufficient. I have to extract (somehow) from view the biggest value and rotate conditionally (only If dim Y > dim in X). This rotation based on this condition have to be done automatically. Again, thank you for interest.

JA
JA 2017-11-06

I used the following method for a cutsheet macro that I wrote a few months ago. I don't want to post the whole thing but you can get an idea of the approach from these snippets.

I chose to use the bounding box of the model in the view rather than the view's border dimensions. The view's border dimensions will change when annotations are added to the view so it isn't a reliable reference. With the model's bounding box, you can make up some convention for which dimension should be larger (with respect to the drawing's coordinate system). i.e. "x" should be larger than "y". For any particular view, you can check if the bounding box, when rotated by the ModeltoViewTransform, satisfies your criteria. If it does not, you can rotate the view. If you put that check and the rotation in a loop, it will converge to the orientation that you desire.

This is VBA but I think it is fairly readable and conveys the intent.

<p>Dim needs_rotate        As Boolean</p><p>Dim prescribed_rotation As Integer 'lazy enumeration</p><p>needs_rotate = True</p><p>While needs_rotate</p><p>    prescribed_rotation = prescribeRotation(swView)</p><p>    If prescribed_rotation = -1 Then</p><p>        needs_rotate = False</p><p>    ElseIf prescribed_rotation = 0 Then</p><p>        Set swView = rotateViewX(swView)</p><p>    ElseIf prescribed_rotation = 1 Then</p><p>        Set swView = rotateViewY(swView)</p><p>    Else</p><p>        Set swView = rotateViewZ(swView)</p><p>    End If</p><p>Wend</p>

rotateViewX() rotates about the X axis in sheet coordinate by increments of 90 degrees. I wrote a similar routine for X,Y,Z. Z just rotates the view in place but X and Y have to create an unfolded view and then delete the parent to achieve the "rotation". The direction of this rotation is dependent on your graphical projection convention (1st vs 3rd angle). A cleaner way of achieving this would be to assign the model's standard views "Front", "Right", etc.

<p>Private Function rotateViewX(swView As SldWorks.View) As SldWorks.View</p><p>    'Project Up</p><p>    Dim swDraw      As SldWorks.DrawingDoc</p><p>    Dim newView     As SldWorks.View</p><p>    Dim position_x  As Double</p><p>    Dim position_y  As Double</p><p>    Dim attempts  As Integer</p><p>    </p><p>    Set swDraw = pDrawModel</p><p>    moveViewCenterToPosition swView, 0, 0</p><p>    position_x = 0#</p><p>    position_y = 1#</p><p>    Set newView = Nothing</p><p>    attempts = 0</p><p>    While newView Is Nothing 'this is pretty sloppy, sometimes it would fail to unfold</p><p>        attempts = attempts + 1</p><p>        DoEvents</p><p>        swDraw.Extension.SelectByID2 swView.GetName2, "DRAWINGVIEW", 0, 0, 0, True, 0, Nothing, 0</p><p>        Set newView = swDraw.CreateUnfoldedViewAt3(position_x, position_y, 0#, True)</p><p>        If attempts > 20 Then</p><p>            MsgBox ("Unfolding of Drawing View (" & swView.GetName2 & ") failed after " & attempts & " attempts." & Chr(13) & "Ending Macro")</p><p>            unloadAll</p><p>        End If</p><p>    Wend</p><p>    </p><p>    deleteView swDraw, swView</p><p>    Set rotateViewX = newView</p><p>End Function</p>

Prescriberotation just determines which axis the view needs to rotate about to achieve the convention that I made up (L >= H  >= W)

<p>Private Function prescribeRotation(swView As SldWorks.View) As Integer</p><p>    '-1: None</p><p>    ' 0: X</p><p>    ' 1: Y</p><p>    ' 2: Z</p><p>    </p><p>    Dim swBody          As SldWorks.Body2</p><p>    Dim swBodyVect      As SldWorks.MathVector</p><p>    Dim arrayData(2)    As Double</p><p>    Dim body_box        As Variant</p><p>    Dim x               As Double</p><p>    Dim y               As Double</p><p>    Dim z               As Double</p><p>    </p><p>    Set swBody = swView.bodies(0)</p><p>    body_box = swBody.GetBodyBox</p><p>    arrayData(0) = Abs(body_box(3) - body_box(0))</p><p>    arrayData(1) = Abs(body_box(4) - body_box(1))</p><p>    arrayData(2) = Abs(body_box(5) - body_box(2))</p><p>    Set swBodyVect = pMathUtil.CreateVector(arrayData)</p><p>    Set swBodyVect = swBodyVect.MultiplyTransform(swView.ModelToViewTransform)</p><p>    x = Abs(swBodyVect.arrayData(0))</p><p>    y = Abs(swBodyVect.arrayData(1))</p><p>    z = Abs(swBodyVect.arrayData(2))</p><p>    If getMaxComponent(x, y, z) = 0 Then</p><p>        If y >= z Then</p><p>            prescribeRotation = -1</p><p>        Else</p><p>            prescribeRotation = 0</p><p>        End If</p><p>    ElseIf getMaxComponent(x, y, z) = 1 Then</p><p>        prescribeRotation = 2</p><p>    Else</p><p>        prescribeRotation = 1</p><p>    End If</p><p>End Function</p>
AL
AL 2017-11-06

Thank you John very much appreciated for example. I will look into it to understand it better. I already have the code for flat-pattern, add dimension automatically and get info from and put them onto sheet. Your approach is new, seems a good one.

AL
AL 2017-11-06

I think once the sheet metal is shown and annotated onto sheet I can get (through loop) the dimensions shown on sheet, get the position of x and y (z will be always 0) and determine from there (in respect of sheet coordinate) to rotate or not.

Its the plan for now.

values to be get only for example

boolstatus = swDrawing.Extension.SelectByID2(@"Line1@Bounding-Box" + a + "@" + Path.GetFileNameWithoutExtension(filename) + "@Drawing View1", "EXTSKETCHSEGMENT", 0.141521430495844, 7.44789415734074E-02, 0, false, 0, null, 0);