.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

DeepCloneObjects insertion point problem

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
Rob.O
1289 Views, 6 Replies

DeepCloneObjects insertion point problem

Hi All,

 

This is related to my other post found here (but I think it's a differnt problem):

 

http://forums.autodesk.com/t5/NET/Wblock-to-new-file-insertion-point-problem/td-p/3252602

 

I am using DeepCloneObjects to create a new block from existing objects (in this case a rectangle (polyline)).  It seems pretty straight forward and everything seems to be working with one expection that is not evident until you create a wblock out of the newly created block.

 

If I use my code (see below) to create a new block (blockTableRecord) from an existing polyline, everything looks good when you actually insert it into the drawing.  The insertion point appears in the correct position (selected position in code), etc...  BUT... if you wblock the inserted block out to a new file, you will notice upon opening the wblocked file that the point selected as the insertion point (with the code) is not at 0,0,0 as you would expect.  It seems to use the insertion point picked from the blocks original location.

 

If you use the same selected object and create the block manually, pick the same insertion point as you did with the code, then wblock the newly create block out to a file and open it, you will see that the insertion point is at the correct location (0,0,0).

 

I am either missing something important (which is probably the case) or this is a bug in the API.  Please help... this is driving me nuts!

 

Thanks!

 

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

        '' Get the current document and database
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim acCurDb As Database = acDoc.Database
        Dim acEd As Editor = DocumentManager.MdiActiveDocument.Editor

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

            Try
                Dim acPrmpt As New EditorInput.PromptSelectionOptions
                acPrmpt.AllowDuplicates = False
                acPrmpt.SinglePickInSpace = True
                acPrmpt.SingleOnly = True
                acPrmpt.AllowSubSelections = False
                acPrmpt.RejectObjectsFromNonCurrentSpace = True
                acPrmpt.MessageForAdding = (vbLf & "Select polyline:")

                Dim acTypdVal(0) As TypedValue
                acTypdVal(0) = New DatabaseServices.TypedValue(0, "LWPOLYLINE")
                Dim myFilterPl As New EditorInput.SelectionFilter(acTypdVal)
                Dim acPrmpt_res As PromptSelectionResult
                acPrmpt_res = acEd.GetSelection(acPrmpt, myFilterPl)

                '' If selection is ok, then continue
                If acPrmpt_res.Status = PromptStatus.OK Then

                    Dim objIds As ObjectId() = acPrmpt_res.Value.GetObjectIds()
                    If objIds.Length = 0 Then
                        Return
                    End If

                    Dim ppo As New PromptPointOptions(vbLf & "Pick block insertion point: ")
                    Dim ppo_res As PromptPointResult = acEd.GetPoint(ppo)
                    If ppo_res.Status <> PromptStatus.OK Then
                        Return
                    End If

                    Dim insPnt As Point3d = ppo_res.Value
                    Dim acBlkTbl As BlockTable = DirectCast(acCurDb.BlockTableId.GetObject(OpenMode.ForWrite), BlockTable)

                    Dim blkName As String = "TESTBLOCK"

                    Dim objIdColl As New ObjectIdCollection()
                    For Each objId As ObjectId In objIds
                        objIdColl.Add(objId)
                    Next

                    Dim newBtr As New BlockTableRecord()
                    acBlkTbl.Add(newBtr)
                    newBtr.Name = blkName
                    newBtr.Origin = insPnt
                    newBtr.BlockScaling = BlockScaling.Uniform
                    newBtr.Explodable = True
                    acTrans.AddNewlyCreatedDBObject(newBtr, True)

                    Dim map As New IdMapping()
                    acCurDb.DeepCloneObjects(objIdColl, newBtr.ObjectId, map, False)
                    Dim coll As New ObjectIdCollection()

                    For Each pair As IdPair In map
                        If pair.IsPrimary Then
                            Dim Ent As Entity = DirectCast(acTrans.GetObject(pair.Value, OpenMode.ForWrite), Entity)
                            If Ent IsNot Nothing Then
                                If (TypeOf (Ent) Is AttributeDefinition) Then
                                    Dim attDef As AttributeDefinition = DirectCast(Ent, AttributeDefinition)
                                    attDef.LockPositionInBlock = True
                                End If
                                coll.Add(Ent.ObjectId)
                            End If
                        End If
                    Next
                    newBtr.AssumeOwnershipOf(coll)

                End If

            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                MsgBox("An error has occured. Please contact your system administrator." & vbCr & _
                ex.Message & vbCr & ex.StackTrace)
            End Try

            acTrans.Commit()
        End Using

    End Sub

 

6 REPLIES 6
Message 2 of 7
Alfred.NESWADBA
in reply to: Rob.O

Hi,

 

just to make sure: you know the sysvar INSBASE (>>>details<<<)? It could be at another value than 0,0,0 and that may help you when wblocking in your code.

Or the other way (what I would prefer) would be to move the objects after wblockclone... in the destination file to 0,0,0.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 7
Rob.O
in reply to: Alfred.NESWADBA

Hi Alfred,

 

Before I posted this I did some searching and came across previous posts (one of yours I beleive) to check the INSBASE sysvar.  I did and it was the same in both drawings.  In any case, both blocks were created in the same drawing (with code and without) and the results were the same. So I would not think INSBASE would affect them differently from within the same drawing.

 

I did try moving the objects in the source file (using TransformBy) with my wblock code, but the problem was the same.

 

I thought my code (from link) that wblocks the block to a new file was the problem, but I have completely ruled that out as this problem occurs when manually creating the wblock on a block created with the DeepCloneObjects code or using my wblock routine on block created with the DeepCloneObjects code.

 

Thanks!

 

Rob

Message 4 of 7
Rob.O
in reply to: Rob.O

So after doing some more reading about the .origin propertly of the BlockTableRecord class, I read this statement (from the ObjectARX SDK)... "It is highly recommended that this point be left at (0,0,0)"

 

As you can see in my code above I was changing the .origin property to the user picked point.

 

So... I changed the .origin property back to 0,0,0 and took a tip from Alfred and actually moved my objects in the block creation process to 0,0,0, before creating the block.  I then deleted the original objects and inserted the block reference to the point the user originally picked.

 

After testing it out and checking the wblock to see if it was created at the proper 0,0,0 location in the new file, I can report that all works as expected! Robot Very Happy

 

I had previously tried to move the blocked object with my wblock code prior to "exporting" the block out to a new file, but that did not work.  I think I was on the right track, but with the wrong approach.

 

Thanks for getting me back on track Alfred! Robot wink

 

I hope this helps someone else that may run into this issue!

 

Edit: spelling

Message 5 of 7
Alfred.NESWADBA
in reply to: Rob.O

Thank's for your feedback!

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 6 of 7
Rob.O
in reply to: Alfred.NESWADBA

No problem Alfred!  I am always happy to post code/resolutions that work so someone else can use it if they run into the same problem. I usually don't have very good code though so it's usually not worth posting! Haha.

 

Something funny I just discovered... You have been helping me with my poor programming for almost 10 years now.  I just found this thread: http://forums.autodesk.com/t5/Visual-Basic-Customization/Text-Box-Scroll-bars/m-p/298127

 

I have changed my screen name a couple of times so it looks like a different person.  I wish I had kept working on VBA for the last several years... it would probbaly make me a better VB.NET programmer! 🙂  Anyway, thanks for all the help!

 

By the way... does anyone know whatever happened to Frank Oquendo? He used to help me with VBA all the time, but I have not seen him around in a while.

 

Rob

Message 7 of 7
agaalen
in reply to: Rob.O

Thanks!!! I could not figure out wat as wrong with My code untill I read this post.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost