Author Topic: Move Entities  (Read 2446 times)

0 Members and 1 Guest are viewing this topic.

cannorth

  • Guest
Move Entities
« on: February 14, 2013, 01:27:42 PM »
Hello,

  I've tried some code to move entities.  For some reason, it removes the entities I want to delete, but it doesn't show them in the moved position.  Here's the code:

Code: [Select]
        With trans
            If bool_had_program_info Then
                Dim new_height As Double = dist + padding * 2.0
                Dim height_difference As Double, height_threshold As Double
                Dim block_name As String, class_block As clsTBlockComponent = Nothing
                Dim cur_block_name_string As String
                Dim blockRef As Autodesk.AutoCAD.DatabaseServices.BlockReference = well_name_blockRef
                Dim id_list2 As New ObjectIdCollection

                height_difference = new_height - old_height
                height_threshold = well_name_blockRef.Position.Y

                For Each blockRef In blockRef_list_copy
                    id_list2.Add(blockRef.Id)
                Next

                Dim drawables As List(Of Drawable) = CreateTransGraphics(trans, id_list2)

                i = 0

                For Each blockRef In blockRef_list_copy
                    For j = 0 To block_list.Count - 1
                        block_name = blockRef_list_name.Item(i)
                        class_block = SearchClassBlock(block_name, block_list)
                    Next

                    If (blockRef.Position.X = well_name_blockRef.Position.X) And (blockRef.Position.Y = well_name_blockRef.Position.Y) Then
                        Dim new_pt As New Autodesk.AutoCAD.Geometry.Point3d(blockRef.Position.X, blockRef.Position.Y + height_difference, 0)
                        block_name = blockRef_list_name.Item(i)

                        Dim id_list As New ObjectIdCollection

                        trans.GetObject(well_name_blockRef.Id, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)
                        well_name_blockRef.Erase()
                        id_list.Add(well_name_blockRef.Id)

                        MoveEntities(trans, blockRef.Position, new_pt, id_list)
                        '                   insertSubBlockReorder(new_pt, block_name, new_height, True, class_block, bt, trans, doc)

                        id_list.Clear()
                    ElseIf (blockRef.Position.Y >= height_threshold) Then
                        Dim new_pt As New Autodesk.AutoCAD.Geometry.Point3d(blockRef.Position.X, blockRef.Position.Y + height_difference, 0)
                        block_name = blockRef_list_name.Item(i)

                        Dim id_list As New ObjectIdCollection()

                        trans.GetObject(blockRef.Id, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)
                        blockRef.Erase()
                        id_list.Add(well_name_blockRef.Id)

                        MoveEntities(trans, blockRef.Position, new_pt, id_list)
                        '     insertSubBlockReorder(new_pt, block_name, new_height, False, class_block, bt, trans, doc)

                        id_list.Clear()
                    Else
                        height_limit_sort_list.Add(i, blockRef)
                    End If

                    i = i + 1
                Next

                ClearTransGraphics(drawables)

                trans.Commit()
                trans.Dispose()
            End If


        End With
    Private Function SearchClassBlock(str_block As String, cur_block_list As Generic.List(Of clsTBlockComponent)) As clsTBlockComponent
        Dim cur_block As clsTBlockComponent

        For Each cur_block In cur_block_list
            If cur_block.BlockName = str_block Then
                SearchClassBlock = cur_block
                Exit Function
            End If

            SearchClassBlock = SearchClassBlock(str_block, cur_block.SubComponents)
        Next
    End Function

    Private Sub UpdateTransGraphics(drawables As List(Of Drawable), curPt As Point3d, moveToPt As Point3d)
        ' Displace each of our drawables



        Dim mat As Matrix3d = Matrix3d.Displacement(curPt.GetVectorTo(moveToPt))



        ' Update their graphics



        For Each d As Drawable In drawables
            Dim e As Entity = DirectCast(d, Entity)

            e.TransformBy(mat)

            TransientManager.CurrentTransientManager.UpdateTransient(d, New IntegerCollection())
        Next
    End Sub



    Private Sub ClearTransGraphics(drawables As List(Of Drawable))

        ' Clear the transient graphics for our drawables


        TransientManager.CurrentTransientManager.EraseTransients(TransientDrawingMode.DirectShortTerm, 128, New IntegerCollection())



        For Each d As Drawable In drawables

            d.Dispose()
        Next

        drawables.Clear()

    End Sub

Thanks,

cannorth

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Move Entities
« Reply #1 on: February 14, 2013, 03:26:57 PM »
Aside from having a very hard time reading vb.net I don't see the MoveEntities function you are calling, further I don't see why you're calling it.  You initialize an ObjectIdCollection with only 1 item in it.  Try:
Code - C#: [Select]
  1. blockRef.TransformBy(Matrix3d.Displacement(blockRef.Position.GetVectorTo(new_pt)))

cannorth

  • Guest
Re: Move Entities
« Reply #2 on: February 15, 2013, 10:46:04 AM »
Sorry, here's the MoveEntities code:

Code: [Select]
    Private Sub MoveEntities(tr As Transaction, basePt As Point3d, moveTo As Point3d, ids As ObjectIdCollection)
        Dim mat As Matrix3d = Matrix3d.Displacement(basePt.GetVectorTo(moveTo))

        For Each id As ObjectId In ids
            Dim ent As Entity = tr.GetObject(id, OpenMode.ForWrite)

            ent.TransformBy(mat)
        Next

    End Sub

cannorth

  • Guest
Re: Move Entities
« Reply #3 on: February 15, 2013, 11:06:11 AM »
WILL Hatch, sorry but your suggestion didn't work.

After I created the blocks, if I try to save the drawing, I get this message:

One or more object in this drawing cannot be saved to the specified format.  The operation was not completed and no file was created.

Thanks,

cannorth

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Move Entities
« Reply #4 on: February 15, 2013, 12:27:23 PM »
The problem is somewhere else, I use that line of code regularly, if you look at what MoveEntities is doing, it's also the exact same thing (except in this case where you're only moving one item it's initializing a For Each statement, as well as wasting time putting things onto the stack during the subroutine call)

I think your problem may be here:

Code - vb.net: [Select]
  1. Dim id_list As New ObjectIdCollection
  2.  
  3.                         trans.GetObject(well_name_blockRef.Id, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)
  4.                         well_name_blockRef.Erase()
  5.                         id_list.Add(well_name_blockRef.Id)
  6.  
  7.                         MoveEntities(trans, blockRef.Position, new_pt, id_list)
  8.                         '                   insertSubBlockReorder(new_pt, block_name, new_height, True, class_block, bt, trans, doc)
  9.  
  10.                         id_list.Clear()

Where you erase the well_name_blockRef then add it to the collection of objects to move.  I think you mean to move blockRef instead