UCS to object

UCS to object

Anonymous
Not applicable
522 Views
6 Replies
Message 1 of 7

UCS to object

Anonymous
Not applicable
Hi all,

Is it possible to rotate the UCS to an object such as a LINE for example. In plane AutoCad you would do 'UCS' 'NEW' 'OBJECT' and then you click an entity.

Many thanks,
P de Boer
0 Likes
523 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
Paul de Boer wrote:

> Hi all,
>
> Is it possible to rotate the UCS to an object such as a LINE for
> example. In plane AutoCad you would do 'UCS' 'NEW' 'OBJECT' and then
> you click an entity.
>
> Many thanks,
> P de Boer

Anything you can do on the command line you can do through the utility
object. you build up the string of text you would type at the command
window and pass it to the vba object and away it goes. ...
0 Likes
Message 3 of 7

Anonymous
Not applicable
> Anything you can do on the command line you can do through the utility
> object. you build up the string of text you would type at the command
> window and pass it to the vba object and away it goes. ...

Which is exactly why you don't do it that way except for extreme situations
of which this isn't one of them. Using sendcommand also means your app
loses all contact with the command so you have no idea when it stops, if an
error occurs, etc.

Paul, here is VB.NET COM InterOp source code that you'll need to revise
for vb6/vba. For example, you need to supply a definition for PI and use
ATN for arctangent instead of Math.ATAN. The code also assumes a valid
cadDoc object is reference [ThisDrawing for vba] and it uses a polyline.
You just need to changeg it for a Line - get the startpoint/endpoint, calc
the angle between then add 90degrees in radians for the UCS "Y" value.

Public Sub UCSByObject(ByVal LwPoly As AcadLWPolyline)
Dim objUCS As AcadUCS
Dim objUCSs As AcadUCSs
Dim objUtil As AcadUtility
Dim dPnt() As Double
Dim dY() As Double
Dim dX() As Double
Dim strPrmt As String
Dim dNormalAngle As Double

objUCSs = cadDoc.UserCoordinateSystems
objUtil = cadDoc.Utility
dNormalAngle = getAngleBetweenTwoPoints(LwPoly.Coordinate(0),
LwPoly.Normal)
dPnt = LwPoly.Coordinate(0)
ReDim Preserve dPnt(2)
dPnt(2) = 0
dX = objUtil.PolarPoint(dPnt, dNormalAngle, 1)
dY = objUtil.PolarPoint(dPnt, dNormalAngle + Degrees2Radians(90), 1)
objUCS = objUCSs.Add(dPnt, dX, dY, "myUCS")
cadDoc.ActiveUCS = objUCS
End Sub

Public Function getAngleBetweenTwoPoints(ByVal dPT1 As Object, ByVal dPT2
As Object) As Double
'ArcTangent
Dim deltaX1 As Double = dPT1(0) - dPT2(0)
Dim deltaY1 As Double = dPT1(1) - dPT2(1)
Dim calcAngle As Double
If deltaX1 = 0 Then
'on vertical axis
If deltaY1 > 0 Then
calcAngle = Math.PI / 2
ElseIf deltaY1 < 0 Then
calcAngle = 3 / 2 * Math.PI
Else
'Zero-length vector
Return 0
Exit Function
End If
ElseIf deltaX1 > 0 Then
'quadrants I and IV
If deltaY1 >= 0 Then 'quadrant I
calcAngle = Math.Atan(deltaY1 / deltaX1)
Else 'quadrant IV
calcAngle = 2 * Math.PI + Math.Atan(deltaY1 / deltaX1)
End If
ElseIf deltaX1 < 0 Then
'quadrants II and III
calcAngle = Math.PI + Math.Atan(deltaY1 / deltaX1)
End If
Return calcAngle
End Function

Public Function Degrees2Radians(ByVal dValue As Double)
Degrees2Radians = dValue * Math.PI / 180
End Function

-- Mike
___________________________
Mike Tuersley
___________________________
the trick is to realize that there is no spoon...
0 Likes
Message 4 of 7

Anonymous
Not applicable
Mike thanks for your time, must say looks a lot of code for something easy in autolisp, but anyway thanks for the code.

Many thanks,
P de Boer
0 Likes
Message 5 of 7

Anonymous
Not applicable
No problem, good luck. The reason there is more code is because lisp is a
macro language that uses the command interpreter and lets you hook into
its commands. With this, we build our own command - real programming 😉

-- Mike
___________________________
Mike Tuersley
___________________________
the trick is to realize that there is no spoon...
0 Likes
Message 6 of 7

Anonymous
Not applicable
That's an interesting perspective Mike. I think the same thing sometimes, but have always wondered why they (adesk) didn't include more methods, properties, and functions that simulate the native commands (checking the WCS is a fine example).
0 Likes
Message 7 of 7

Anonymous
Not applicable
A line has only 2 meaningful points; to define an UCS you need 3 points; therefore you may have infinite solutions. You have to impose your general and consistent rule; for instance: to have always the UCS_origin in the .StartPoint, the X axis collinear with your line, the Y axis horizontal, Z axis going upward.
A circle or an arc define a plan, but there are infinite solutions again. Your consistent rule may be: to use their .Normal as Z axis and the .Center as Origin, and to have the X axis going in the same direction of the WCS_X axis.
For an AcadPoint you may impose - for instance - that the UCS_Origin has always to be in the AcadPoint, and the axes have to be parallel to the WCS axes.

I have not pay attention to the general rules that AutoCAD follows when you use UCS New OBject, but it isn't sure that those rules are always the best solution for your application; maybe your application needs other rules. For instance: to have the Origin in the middle of the selected line, the UCS_Z axis going downward, the X vector collinear with the line and going towards the higher point of your line, the Y axis horizontal. VBA (and even more other more complex languages) let you do what you want; therefore your problem is to know what you want :-), I think 🙂
0 Likes