.NET

Reply
Valued Contributor
architectOfIdeas
Posts: 75
Registered: ‎08-10-2007
Message 1 of 8 (387 Views)
Accepted Solution

creating xline from block reference position in custom ucs

387 Views, 7 Replies
03-14-2013 09:18 AM

so... i'm looking to select a block reference, take it's wcs position, translate the coordinates to the current ucs and then create an xline using those translated coordinates. simple, right? 

 

i simplified some code to test that i could properly build an xline in a current ucs. i just threw in static point3d values instead of grabbing them from another object. it worked as expected:

 

        <CommandMethod("test")> _
        Public Sub test()

            'get the active document, editor and database
            Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
            Dim acDocEd As Editor = acDoc.Editor
            Dim acCurDb As Database = acDoc.Database

            'start a transaction
            Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

                'open the table for read
                Dim acBlkTbl As BlockTable
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

                'open the record for write
                Dim acBlkTblRec As BlockTableRecord
                acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

                'define the current ucs
                Dim ucsDesign As Matrix3d = acDocEd.CurrentUserCoordinateSystem

                'define the points
                Dim ptBase As Point3d = New Point3d(0, 0, 0)
                Dim ptSecond As Point3d = New Point3d(0, 1, 0)

                'translate the points
                Dim ptUcsBase As Point3d = ptBase.TransformBy(ucsDesign)
                Dim ptUcsSecond As Point3d = ptSecond.TransformBy(ucsDesign)

                'write the result to the command line
                acDocEd.WriteMessage(vbLf & ptUcsBase.ToString() & vbLf & ptUcsSecond.ToString())

                'create the xline
                Dim acXline As Xline = New Xline()
                acXline.BasePoint = ptUcsBase
                acXline.SecondPoint = ptUcsSecond

                'add the xline to the drawing
                acBlkTblRec.AppendEntity(acXline)
                acTrans.AddNewlyCreatedDBObject(acXline, True)

                'commit the changes
                acTrans.Commit()

            End Using
End Sub

here, i revised the code so that a block reference's objectid is passed from another procedure, the origin is determined, translated to the current ucs and the points are tailored for the xline's basepoint and secondpoint. i threw in a few writemessages to ensure that the coordinates were indeed what i was looking for and they match up, but for some reason, the xline now fails to create itself.

 

'define the current ucs
            Dim ucsDesign As Matrix3d = acDocEd.CurrentUserCoordinateSystem

            'start a transaction
            Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

                'open the connector block for read
                Dim acBlock As BlockReference = acTrans.GetObject(idConnector, OpenMode.ForRead)

                'save connector origin
                Dim ptOrigin As Point3d = acBlock.Position

                'translate the point to the current ucs
                Dim ptUcsOrigin As Point3d = ptOrigin.TransformBy(ucsDesign.Inverse)

                'show me the block position
                acDocEd.WriteMessage(vbLf & ptOrigin.ToString())
                acDocEd.WriteMessage(vbLf & ptUcsOrigin.ToString())

                'open the table for read
                Dim acBlkTbl As BlockTable
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

                'open the record for write
                Dim acBlkTblRec As BlockTableRecord
                acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

                'define the points
                Dim ptBase As Point3d = ptUcsOrigin
                Dim ptSecond As Point3d = New Point3d(ptUcsOrigin.X, ptUcsOrigin.Y + 1.0, ptUcsOrigin.Z)

                'show me the points
                acDocEd.WriteMessage(vbLf & ptBase.ToString() & vbLf & ptSecond.ToString())

                'create the xline
                Dim acXline As Xline = New Xline()
                acXline.BasePoint = ptBase
                acXline.SecondPoint = ptSecond

                'add the xline to the drawing
                acBlkTblRec.AppendEntity(acXline)
                acTrans.AddNewlyCreatedDBObject(acXline, True)

                'commit the changes
                acTrans.Commit()

            End Using

 i'm hoping somebody could point out a simple mistake that i'm making and end this headache. i've rearranged this code multiple times, tried a couple different approaches, scoured the forums and even resorted to the less-than-elegant:

 

            'create the xline
            Dim cmdString As String = "._xline " & ptUcsOrigin.X & "," & ptUcsOrigin.Y & "," & ptUcsOrigin.Z & " " & ptUcsOrigin.X & "," & ptUcsOrigin.Y + 1.0 & "," & ptUcsOrigin.Z & "  "
            acDoc.SendStringToExecute(cmdString, True, False, False)

 but i need to use the xline object after i've created it, so this sendstringtoexecute crap won't work. 

Class Xline needed coordinates in WCS. So after all calculation you have to translate coordinates from UCS to WCS.

Valued Contributor
architectOfIdeas
Posts: 75
Registered: ‎08-10-2007
Message 2 of 8 (353 Views)

Re: creating xline from block reference position in custom ucs

03-14-2013 10:49 AM in reply to: architectOfIdeas

there is one difference between the code snippets that's probably worth mentioning. the simplified test code that worked didn't include:

 

Imports Autodesk.Gis.Map

 it's used in another class, but in the same module. is it possible that this interferes? 

Valued Contributor
architectOfIdeas
Posts: 75
Registered: ‎08-10-2007
Message 3 of 8 (314 Views)

Re: creating xline from block reference position in custom ucs

03-19-2013 07:29 AM in reply to: architectOfIdeas

nobody? bueler? 

Moderator
Alexander.Rivilis
Posts: 1,449
Registered: ‎04-09-2008
Message 4 of 8 (309 Views)

Re: creating xline from block reference position in custom ucs

03-19-2013 07:41 AM in reply to: architectOfIdeas

Maybe instead of

acXline.BasePoint = ptBase
acXline.SecondPoint = ptSecond

using:

acXline.BasePoint = ptBase.TransformBy(ucsDesign)
acXline.SecondPoint = ptSecond.TransformBy(ucsDesign)

?


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Valued Contributor
architectOfIdeas
Posts: 75
Registered: ‎08-10-2007
Message 5 of 8 (304 Views)

Re: creating xline from block reference position in custom ucs

03-19-2013 08:16 AM in reply to: Alexander.Rivilis

thanks for the reply!

 

i'm guessing the intention would be to remove unnecessary variables?

 

i've tried rearranging the code to pass the information as direct as possible and it still fails. 

 

'create the xline
                Dim acXline As Xline = New Xline()
                acXline.BasePoint = acBlock.Position.TransformBy(ucsDesign.Inverse)
                acXline.SecondPoint = New Point3d(acBlock.Position.X, acBlock.Position.Y + 1.0, acBlock.Position.Z)

 the frustrating thing is, i'm able to see my result at the command line. i know the information is being passed, it's just not being used by either the xline class or the appendentity/addnewlycreateddboject methods.

Moderator
Alexander.Rivilis
Posts: 1,449
Registered: ‎04-09-2008
Message 6 of 8 (302 Views)

Re: creating xline from block reference position in custom ucs

03-19-2013 08:24 AM in reply to: architectOfIdeas

Class Xline needed coordinates in WCS. So after all calculation you have to translate coordinates from UCS to WCS.


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Valued Contributor
architectOfIdeas
Posts: 75
Registered: ‎08-10-2007
Message 7 of 8 (298 Views)

Re: creating xline from block reference position in custom ucs

03-19-2013 08:29 AM in reply to: Alexander.Rivilis

that would be what i'm misunderstanding! i'll toy around with it.

Valued Contributor
architectOfIdeas
Posts: 75
Registered: ‎08-10-2007
Message 8 of 8 (273 Views)

Re: creating xline from block reference position in custom ucs

03-21-2013 02:39 PM in reply to: Alexander.Rivilis

thank you very much alexander. dealing with wcs coordinates did the trick. i was also missing a very important acTrans.Commit() buried in some nested transactions!!

 

'start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

     'save connector origin
     Dim ptBase As Point3d = acBlock.Position

     'open the table for read
     Dim acBlkTbl As BlockTable
     acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

     'open the record for write
     Dim acBlkTblRec As BlockTableRecord
     acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

     'define the second point, by translating it into the ucs and modifying
     Dim ptRef As Point3d = ptBase.TransformBy(ucsDesign.Inverse)
     Dim ptUcsSecond As Point3d = New Point3d(ptRef.X, ptRef.Y + 1.0, ptRef.Z)

     'translate the modified point back into wcs
     Dim ptSecond As Point3d = ptUcsSecond.TransformBy(ucsDesign)

     'show me the points
     acDocEd.WriteMessage(vbLf & ptBase.ToString() & vbLf & ptSecond.ToString())

     'create the xline
     Dim acXline As Xline = New Xline()
     acXline.BasePoint = ptBase
     acXline.SecondPoint = ptSecond

     'add the xline to the drawing
     acBlkTblRec.AppendEntity(acXline)
     acTrans.AddNewlyCreatedDBObject(acXline, True)

     'commit the changes
     acTrans.Commit()

End Using

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!