Author Topic: Copy Dynamic Block inside a drawing?  (Read 1541 times)

0 Members and 1 Guest are viewing this topic.

pedroantonio

  • Guest
Copy Dynamic Block inside a drawing?
« on: May 05, 2013, 08:23:28 AM »
I need to copy an existed dynamic block to another place exactly as it is: with the same Anno scale, the same visible state, the same visible attributes... etc.
I am almost there, but... attributes are refusing to go to the new position. They are copied but not moved to position of new block (they are copied exactly over the old block position and not somewhere around 0,0,0).
Any help?

Code: [Select]
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Using tr As Transaction = db.TransactionManager.StartTransaction()
   'select block that I need to copy
   Dim idBlock As New ObjectIdCollection()
   Dim peo As New PromptEntityOptions(Environment.NewLine & "Select Block to Copy")
   peo.SetRejectMessage(Environment.NewLine & "Not a Block Reference")
   peo.AddAllowedClass(GetType(BlockReference), True)
   idBlock.Add(doc.Editor.GetEntity(peo).ObjectId)

   Using map As New IdMapping()
      Dim newBref As BlockReference = Nothing
      db.DeepCloneObjects(idBlock, db.CurrentSpaceId, map, False)
     
      For Each idP As IdPair In map
         If idP.Value.ObjectClass = RXClass.GetClass(GetType(BlockReference)) Then
            newBref = DirectCast(tr.GetObject(idP.Value, OpenMode.ForWrite), BlockReference)
            newBref.Position = New Point3d(0, 0, 0)
         End If
      Next

   End Using 'map

   tr.Commit()
End Using 'tr

Thanks in advance

TheMaster

  • Guest
Re: Copy Dynamic Block inside a drawing?
« Reply #1 on: May 05, 2013, 09:30:17 PM »
Your code is changing the position of the BlockReference, which isn't going to affect the attributes.

You need to use TransformBy() to transform the BlockReference and its attributes to the new location.
If the attributes are in their default positions (where they are when the block was first inserted), then they may transform along with the block reference, otherwise they stay put and must be transformed individually. At least, that's how it should work.

pedroantonio

  • Guest
Re: Copy Dynamic Block inside a drawing?
« Reply #2 on: May 06, 2013, 12:48:23 PM »
I dont know how to do that.
What I have tryied is here but it does not work:

Code: [Select]
'...
Using map As New IdMapping()
      Dim newBref As BlockReference = Nothing
      db.DeepCloneObjects(idBlock, db.CurrentSpaceId, map, False)
     
      For Each idP As IdPair In map
         If idP.Value.ObjectClass = RXClass.GetClass(GetType(BlockReference)) Then
            newBref = DirectCast(tr.GetObject(idP.Value, OpenMode.ForWrite), BlockReference)
            newBref.Position = New Point3d(0, 0, 0)
         End If
      Next

      '= NEW =============
      Dim newAref As AttributeReference = Nothing
      If (idP.Value.ObjectClass = RXClass.GetClass(GetType(AttributeReference))) Then
         newAref = DirectCast(tr.GetObject(idP.Value, OpenMode.ForWrite), AttributeReference)
         newAref.Position = New Point3d(0, 0, 0)
         newAref.AdjustAlignment(db)
      End If

   End Using 'map
'...