Insert block in UCS with correct rotation

Insert block in UCS with correct rotation

Anonymous
Not applicable
1,144 Views
10 Replies
Message 1 of 11

Insert block in UCS with correct rotation

Anonymous
Not applicable
When I try to insert a block using "InsertBlock" in VB6 and the current UCS is not WCS, the rotation of the block becames wrong, even if I trasform (transformby) the insertion point.

How can I solve it ? Is it a Autocad bug ???

I read all the messages about it in this forum but none had the answer...

Thanks !!!

Leonardo Roncetti
0 Likes
1,145 Views
10 Replies
Replies (10)
Message 2 of 11

Anonymous
Not applicable
is it right if the WCS is current?

wrote in message news:5211042@discussion.autodesk.com...
When I try to insert a block using "InsertBlock" in VB6 and the current UCS
is not WCS, the rotation of the block becames wrong, even if I trasform
(transformby) the insertion point.

How can I solve it ? Is it a Autocad bug ???

I read all the messages about it in this forum but none had the answer...

Thanks !!!

Leonardo Roncetti
0 Likes
Message 3 of 11

Anonymous
Not applicable
The InsertBlock method only works with WCS coordinates. See help on the InsertBlock method.

You may need to use the TranslateCoordinates method to get the proper WCS for your current UCS points.
0 Likes
Message 4 of 11

Anonymous
Not applicable
You may need to adjust the .Normal vector of the BlockRef to get the rotation right if you aren't in WCS also.
0 Likes
Message 5 of 11

Anonymous
Not applicable
When I try to adjust the .Normal vector, the block moves instead of rotating.

When I apply the Transformby method the angle is not ok...
0 Likes
Message 6 of 11

Anonymous
Not applicable
It would help if you posted your code....
0 Likes
Message 7 of 11

Anonymous
Not applicable
Does the insertion point move? I'd expect other portions of the block to move, because when you change the normal vector, you are
rotating the objects about the insertion point, but the insertion point should not move.


wrote in message news:5211894@discussion.autodesk.com...
When I try to adjust the .Normal vector, the block moves instead of rotating.

When I apply the Transformby method the angle is not ok...
0 Likes
Message 8 of 11

Anonymous
Not applicable
Here goes my code:
......
Set objBlock = moSpace.InsertBlock(pins, name_block, 1, 1, 1, 0) '' Pins is already tranformed to WCS

'' In the following code I tried to change de Normal vector
'' Dim norm As Variant
'' Dim nVec(x To Z) As Double
'' nVec(0) = 0
'' nVec(1) = 0
'' nVec(2) = 1
'' norm = objBlock.Normal
'' objBlock.Normal = nVec

Set ucsObj = acadDoc.ActiveUCS '' I´m using a UCS, not WCS
TransMatrix = ucsObj.GetUCSMatrix()
Call objBlock.TransformBy(TransMatrix)
Call objBlock.Update

After this, the angle is completely different form the UCS....
The insertion point is on the right position... Message was edited by: LeoRoncetti
0 Likes
Message 9 of 11

Anonymous
Not applicable
Transforms work to make the angle correct also. See below.

Sub Test()
CreateUCS

Dim pt1(0 To 2) As Double
pt1(0) = 0#: pt1(1) = 0#: pt1(2) = 0# 'WCS point

'Below insertion point and rotatation are in WCS
Dim myBlockRef As AcadBlockReference
Set myBlockRef = ThisDrawing.ModelSpace.InsertBlock(pt1, "Test", 1#, 1#,
1#, 0#)

'Fix insertion to match current UCS
If ThisDrawing.GetVariable("UCSName") <> "" Then
Dim ucsMatrix As Variant
ucsMatrix = ThisDrawing.ActiveUCS.GetUCSMatrix

myBlockRef.TransformBy ucsMatrix
End If
End Sub

Private Sub CreateUCS()
Dim ucsOrg(0 To 2) As Double
ucsOrg(0) = 3#: ucsOrg(1) = 3#: ucsOrg(2) = 3# 'WCS point

With ThisDrawing.Utility
Dim ucsXPt As Variant
ucsXPt = .PolarPoint(ucsOrg, .AngleToReal("45", acDegrees), 1#) 'vector

Dim ucsYPt As Variant
ucsYPt = .PolarPoint(ucsOrg, .AngleToReal("135", acDegrees), 1#) 'vector
End With

Dim myUCS As AcadUCS
Set myUCS = ThisDrawing.UserCoordinateSystems.Add(ucsOrg, ucsXPt, ucsYPt,
"Test")

ThisDrawing.ActiveUCS = myUCS
End Sub


--
R. Robert Bell


wrote in message news:5212095@discussion.autodesk.com...
Here goes my code:
......
Set objBlock = moSpace.InsertBlock(pins, name_block, 1, 1, 1, 0) '' Pins is
already tranformed to WCS

'' In the following code I tried to change de Normal vector
'' Dim norm As Variant
'' Dim nVec(x To Z) As Double
'' nVec(0) = 0
'' nVec(1) = 0
'' nVec(2) = 1
'' norm = objBlock.Normal
'' objBlock.Normal = nVec

Set ucsObj = acadDoc.ActiveUCS '' I´m using a UCS, not WCS
TransMatrix = ucsObj.GetUCSMatrix()
Call objBlock.TransformBy(TransMatrix)
Call objBlock.Update

After this, the angle is completely different form the UCS....
The insertion point is on the right position...

Message was edited by: LeoRoncetti
0 Likes
Message 10 of 11

Anonymous
Not applicable
I guess I'm not sure what you want. Do you want the block's rotation to change when you jump to the UCS?

I was experimenting with the following code, which seems to rotate the block to the UCS plane, but keeps the original insertion
point.

Sub Example_GetUCSMatrix()
' This example creates a new UCS and finds the UCS matrix for it.
' It then creates a circleblock using WCS coordinates and
' transforms the circle for the UCS.

' Define a new UCS and turn on the UCS icon at the origin.
Dim ucsObj As AcadUCS
Dim origin(0 To 2) As Double
Dim xAxisPoint(0 To 2) As Double
Dim yAxisPoint(0 To 2) As Double

origin(0) = 2: origin(1) = 2: origin(2) = 0
xAxisPoint(0) = 3: xAxisPoint(1) = 2: xAxisPoint(2) = 0
yAxisPoint(0) = 2: yAxisPoint(1) = 3: yAxisPoint(2) = 4


' Create the block
Dim blockObj As AcadBlock
Dim insertionPnt(0 To 2) As Double
insertionPnt(0) = 0#: insertionPnt(1) = 0#: insertionPnt(2) = 0#
Set blockObj = ThisDrawing.Blocks.Add(insertionPnt, "CircleBlock")

' Add a circle to the block
Dim circleObj As AcadCircle, lineObj As AcadLine
Dim center(0 To 2) As Double
Dim radius As Double
center(0) = 0: center(1) = 0: center(2) = 0
radius = 1
Set circleObj = blockObj.AddCircle(center, radius)
Set lineObj = blockObj.AddLine(center, yAxisPoint)
Set lineObj = blockObj.AddLine(center, xAxisPoint)


insertionPnt(0) = 2#: insertionPnt(1) = 2#: insertionPnt(2) = 0
'Insert the block twice at the same point
Dim blockRefObj0 As AcadBlockReference, blockRefObj1 As AcadBlockReference

Set blockRefObj0 = ThisDrawing.ModelSpace.InsertBlock(insertionPnt, "CircleBlock", 1#, 1#, 1#, 0)
Set blockRefObj1 = ThisDrawing.ModelSpace.InsertBlock(insertionPnt, "CircleBlock", 1#, 1#, 1#, 0)

'Create the new UCS
ThisDrawing.SendCommand "ucsfollow 1 "
Set ucsObj = ThisDrawing.UserCoordinateSystems.Add(origin, xAxisPoint, yAxisPoint, "UCS1")
ThisDrawing.ActiveUCS = ucsObj
ThisDrawing.ActiveViewport.UCSIconOn = True
ThisDrawing.ActiveViewport.UCSIconAtOrigin = True
ThisDrawing.ActiveViewport = ThisDrawing.ActiveViewport

' Get the UCS transformation matrix
Dim TransMatrix As Variant
TransMatrix = ucsObj.GetUCSMatrix()

' Transform the circle to the UCS coordinates

blockRefObj1.TransformBy (TransMatrix)
blockRefObj1.InsertionPoint = insertionPnt
blockRefObj1.Update


End Sub
0 Likes
Message 11 of 11

DaSteelman
Contributor
Contributor
Hello Leonardo,

Maybe a bit late, but I was struggling with the same problem just now...

I solved the mystery !

My programming background is Delphi, that's why I will not write VBA code.

When inserting a blockreference it will always be placed at the coordinates you provide in WCS, its orientation however is in UCS! When using this method there is no need to switch between UCS and WCS every time.

A transformation with the UCS-matrix will NOT work because your insertion point is not in UCS...

What you'll have to do is:
1) move the block to the origin with
- define a matrix for translation: -x,-y,-z
- transformby(transMatrix)
2) now align the orientation to the WCS
- get the OCS-matrix, this is the UCS-matrix without the translation part
- transformby(inverted(OCS-matrix))
3) move the block to its insertionpoint
- define a matrix for translation: x,y,z
- transformby(transMatrix)

Tadaaa

Hope this can still help you ;o)
0 Likes