Author Topic: Moving Entities For VB.Net project not working  (Read 4580 times)

0 Members and 1 Guest are viewing this topic.

cannorth

  • Guest
Moving Entities For VB.Net project not working
« on: March 18, 2013, 11:32:33 AM »
Hello,

  I have attached a VB.Net project for AutoCAD 2012 with a drawing.  I want the project to stack the drawings one on top of each other in a drawing using a move entities function.  However, the problem is when I use MoveEntities, it has an error.  Can someone please point out the
problem with my coding approaching?

Thanks,

cannorth

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Moving Entities For VB.Net project not working
« Reply #1 on: March 18, 2013, 12:18:06 PM »
First note, could you just post your code inline so I don't have to download your whole 5.5MB project directory to view ~250 lines of code.

Second note, POST THE ERROR!

I'm noticing that you are opening a form from an autocad command, then upon pressing a button.  I think this requires a commandflags.modal in the method declaration.

Also noticing that you're creating all the attribute references twice, and the first time not doing anything with them.  They'll need to be disposed.  If all you're trying to do is test for the "PROGRAMINFO" tag then you don't need to create an AttributeReference, you can test that same property on the AttributeDefinition.
Code - vb.net: [Select]
  1.                             For Each idAtt In btAttRec
  2.                                 Dim ent As Entity
  3.                                 ent = trans.GetObject(idAtt, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
  4.                                 If TypeOf ent Is AttributeDefinition Then
  5.                                     Dim attDef As AttributeDefinition
  6.                                     attDef = CType(ent, AttributeDefinition)
  7.                                     Dim attRef As New AttributeReference()
  8.                                     attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform)
  9.                                     Dim ptBase As New Autodesk.AutoCAD.Geometry.Point3d(blockRef.Position.X + attDef.Position.X, blockRef.Position.Y + attDef.Position.Y, blockRef.Position.Z + attDef.Position.Z)
  10.                                     attRef.Position = ptBase
  11.                                     'attRef.Rotation = attDef.Rotation
  12.  
  13.                                     If attRef.Tag = "PROGRAMINFO" Then
  14.                                         bool_program_info = True
  15.                                     End If
  16.                                 End If
  17.                             Next

I would suggest not using move entites and just use
blockRef.TransformBy(new Matrix3d.Displacement(blockRef.Position.GetVectorTo(pt3D1)))
I'm thinking the error is caused because you're trying to open the blockRef for write and it is not database resident
« Last Edit: March 18, 2013, 12:23:43 PM by WILL HATCH »

cannorth

  • Guest
Re: Moving Entities For VB.Net project not working
« Reply #2 on: March 18, 2013, 12:57:11 PM »
Hello,

  When I use the blockRef.TransformBy function that you recommend, the blocks are not stacked one on top of each other, but overlap.  Why would this happen?

Thanks,

cannorth

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Moving Entities For VB.Net project not working
« Reply #3 on: March 18, 2013, 01:43:55 PM »
Look at how you are working with pt3D1 and height_right.  I'm not going to spoon feed you the solution beyond that.

TheMaster

  • Guest
Re: Moving Entities For VB.Net project not working
« Reply #4 on: March 19, 2013, 01:57:45 AM »
Hello,

  When I use the blockRef.TransformBy function that you recommend, the blocks are not stacked one on top of each other, but overlap.  Why would this happen?

Thanks,

cannorth

TransformBy() performs a transformation that includes displacement (e.g., "move"). You have to define the displacement using Matrix3d.Displacement() and give it the Vector that defines the direction and distance to move. If you do it correctly (sorry, I'm not downloading your attachment), then TransformBy() will move something to where you want it.

It would be helpful if you were to research the basic geometry functions provided by the more commonly-used objects in the Geometry namespace (that would be Point3d, Vector3d, and Matrix3d).

fixo

  • Guest
Re: Moving Entities For VB.Net project not working
« Reply #5 on: March 19, 2013, 06:55:00 AM »
Hello,

  I have attached a VB.Net project for AutoCAD 2012 with a drawing.  I want the project to stack the drawings one on top of each other in a drawing using a move entities function.  However, the problem is when I use MoveEntities, it has an error.  Can someone please point out the
problem with my coding approaching?

Thanks,

cannorth

Try this code, it will move selected objects to origin point,
you can change a new location within the code, tested on A2010 only
 
Code: [Select]
    //using Linq;
       [System.Security.SuppressUnmanagedCodeSecurity]
        [DllImport("acad.exe", EntryPoint = "acedCmd", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        extern static private int acedCmd(IntPtr resbuf);

        [CommandMethod("moveToOrig, mto", CommandFlags.UsePickSet | CommandFlags.Redraw)]
        static public void testMoveToOrigin()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            ResultBuffer rb = new ResultBuffer();
            try
            {

                rb.Add(new TypedValue(5005, "_Zoom"));
                rb.Add(new TypedValue(5005, "_Extents"));
                acedCmd(rb.UnmanagedObject);

                Matrix3d ucs = ed.CurrentUserCoordinateSystem;
                CoordinateSystem3d ccos = ucs.CoordinateSystem3d;
                Point3d orig = ccos.Origin.TransformBy(Matrix3d.Identity);
                // select all objects
                SelectionSet sset = ed.SelectAll().Value;
                if (sset == null) return;
                List<Point3d> pts = new List<Point3d>();
                List<Entity> ents = new List<Entity>();
                Matrix3d mmx = new Matrix3d();
                using (Transaction tr = doc.TransactionManager.StartTransaction())
                {
                    // iterate through selected objects
                    foreach (ObjectId id in sset.GetObjectIds())
                    {

                        Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead, false);

                        Extents3d ext = ent.GeometricExtents;
                        if (ext != null)
                        {
                            Point3d minpt = ext.MinPoint.TransformBy(Matrix3d.Identity);
                            // collect entities to List for the future work
                            ents.Add(ent);
                            pts.Add(minpt);
                        }
                    }
                    // Get most lower left point of screen
                    Point3d xpt = pts.OrderBy(p => p.X).First();// calculate minimal X value
                    Point3d ypt = pts.OrderBy(p => p.Y).First();// calculate minimal Y value
                 Point3d mp = new Point3d(xpt.X, ypt.Y, orig.Z).TransformBy(Matrix3d.Identity);
                    mmx = Matrix3d.Displacement(orig-mp );
                    // iterate through gathrered entities again
                    foreach (Entity e in ents)
                    {
                        e.UpgradeOpen();
                        // apply transformation matrix
                        e.TransformBy(mmx);
                    }

                    tr.Commit();
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                ed.WriteMessage("\n" + ex.Message + "\n" + ex.StackTrace);
            }
            finally
            {
                rb = new ResultBuffer();
                rb.Add(new TypedValue(5005, "_Zoom"));
                rb.Add(new TypedValue(5005, "_Extents"));
                acedCmd(rb.UnmanagedObject);
            }
        }

cannorth

  • Guest
Re: Moving Entities For VB.Net project not working
« Reply #6 on: March 19, 2013, 11:02:10 AM »

When I translate TestMoveToOrigin to VB.Net there's a translation problem. 

This code:
Code: [Select]
Point3d xpt = pts.OrderBy(p => p.X).First();// calculate minimal X value
                    Point3d ypt = pts.OrderBy(p => p.Y).First();// calculate minimal Y value

Has issues in VB.Net because the List data type does not recognize OrderBy as a method.

Thanks,

cannorth

Jeff H

  • Needs a day job
  • Posts: 6144
Re: Moving Entities For VB.Net project not working
« Reply #7 on: March 19, 2013, 12:20:18 PM »
Has issues in VB.Net because the List data type does not recognize OrderBy as a method.

Thanks,

cannorth
Imports System.Linq?

cannorth

  • Guest
Re: Moving Entities For VB.Net project not working
« Reply #8 on: March 19, 2013, 01:29:47 PM »
When I use fixo's function to move in VB.Net, it doesn't work.  It doesn't move anything.

Here's my vb.net code:
Code: [Select]
        Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor
        Dim db As Database = doc.Database
        Dim rb As ResultBuffer = New ResultBuffer()


        Try


            rb.Add(New TypedValue(5005, "_Zoom"))
            rb.Add(New TypedValue(5005, "_Extents"))
            acedCmd(rb.UnmanagedObject)

            Dim ucs As Matrix3d = ed.CurrentUserCoordinateSystem
            Dim ccos As CoordinateSystem3d = ucs.CoordinateSystem3d
            Dim orig As Point3d = pt1
            ' select all objects
            Dim sset As SelectionSet = ed.SelectAll().Value
            If (sset Is Nothing) Then
                Exit Sub
            End If

            Dim pts As New List(Of Point3d)()
            Dim ents As List(Of Entity) = New List(Of Entity)

            Dim mmx As Matrix3d = New Matrix3d()

            Using tr1 As Transaction = doc.TransactionManager.StartTransaction()

                'iterate through selected objects
                For Each id As ObjectId In sset.GetObjectIds()

                    Dim ent As Entity = tr1.GetObject(id, OpenMode.ForRead, False)

                    Dim ext As Extents3d = ent.GeometricExtents
                    If ext <> Nothing Then
                        Dim minpt As Point3d = ext.MinPoint.TransformBy(Matrix3d.Identity)
                        ' collect entities to List for the future work
                        ents.Add(ent)
                        pts.Add(minpt)
                    End If

                Next
                ' Get most lower left point of screen
                Dim xpt As Point3d = pts.OrderBy(Function(p) p.X).First()
                ' calculate minimal X value
                Dim ypt As Point3d = pts.OrderBy(Function(p) p.Y).First()
                ' calculate minimal Y value
                Dim mp As Point3d = New Point3d(xpt.X, ypt.Y, orig.Z).TransformBy(Matrix3d.Identity)
                mmx = Matrix3d.Displacement(orig - mp)
                ' iterate through gathrered entities again
                For Each e As Entity In ents
                    e.UpgradeOpen()
                    ' apply transformation matrix
                    e.TransformBy(mmx)
                Next

                tr1.Commit()
            End Using
        Catch ex As Exception
        End Try

Thanks,

cannorth

 

TheMaster

  • Guest
Re: Moving Entities For VB.Net project not working
« Reply #9 on: March 21, 2013, 01:51:38 AM »
When I use fixo's function to move in VB.Net, it doesn't work.  It doesn't move anything.

Here's my vb.net code:
Code: [Select]

     --------<snip>---------

        Catch ex As Exception
        End Try

Thanks,

cannorth

 

My advice would be to spend a little time on the basics of .NET, and more specifically on error handling, to become more familiar with things like Try/Catch/Finally, so that you would be able to understand why the last two lines of your code, shown above, conceals any errors that might be happening.