Results 1 to 2 of 2

Thread: How to scale geometry in drawing and move attributes? C#

  1. #1
    Member
    Join Date
    2013-01
    Posts
    2
    Login to Give a bone
    0

    Default How to scale geometry in drawing and move attributes? C#

    I am absolute noob in this area but I decided to do something with it. I'd like to achieve this: there is a drawing with a geometry - lets say a circle - and attribute definition. What I like to do is to scale everything in the drawing by 2 and additionaly to move attribute to nearest grid point (if an attribute definition is located in [10.1,14.9] it should be moved to [10,15]. To be honest I know how to create new simple objects, but I dont know how to handle existing stuff and have no idea how to query location from attribute definitions.Any help appreciated! Thanks!
    ps: VS2013/ACAD2015
    example drawings in 2010/2013 format attached.
    Attached Files Attached Files

  2. #2
    Woo! Hoo! my 1st post
    Join Date
    2014-07
    Posts
    1
    Login to Give a bone
    0

    Default Re: How to scale geometry in drawing and move attributes? C#

    One way is that you pass the ObjectIdCollection of the entities you want to edit, the other one is iterating the current block.
    For scaling a moving you can make a function that receive the ObjectIdCollection

    Code:
     public void ScaleAndMove(ObjectIdCollection objIds, double scaleFactor = 2)
            {
                Document currentDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                const double gridSize=5;
                using (Transaction tr = currentDoc.Database.TransactionManager.StartTransaction())
                {
                    try
                    {
                        Entity ent;
                        foreach (ObjectId objId in objIds)
                        {
                            //Open Object as Entity, anything that can be drew is an entity
                            ent = objId.GetObject(OpenMode.ForWrite) as Entity;
                            //Use the center of the entity as scale point
                            Point3d min = ent.GeometricExtents.MinPoint,
                                    max = ent.GeometricExtents.MaxPoint,
                                    mid = new Point3d((min.X + max.X) / 2, (min.Y + max.Y) / 2, (min.Y + max.Y) / 2);
                            //Scale using the selected factor
                            ent.TransformBy(Matrix3d.Scaling(scaleFactor, mid));
                            //If the entity is an attribute definition,
                            //it will be moved to the nearest grip point
                            //Assuming that the grid is size 5
                            if (ent is AttributeDefinition)
                            {
                                AttributeDefinition attDef = ent as AttributeDefinition;
                                //Use the position defined in the attDef
                                Point3d currentPos = attDef.Position;
                                //Gets the nearest point
                                double x = (Math.Round(currentPos.X / gridSize) * gridSize),
                                       y = (Math.Round(currentPos.Y / gridSize) * gridSize), 
                                       z = (Math.Round(currentPos.Z / gridSize) * gridSize);
                                //Move
                                Vector3d vec = currentPos.GetVectorTo(new Point3d(x, y, z));
                                attDef.TransformBy(Matrix3d.Displacement(vec));
                            }
                        }
                        tr.Commit();
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception)
                    {
                        tr.Abort();
                        //Something goes wrong in AutoCAD
                    }
                    catch (System.Exception)
                    {
                        tr.Abort();
                        //Something else goes wrong
                    }
                }
            }

Similar Threads

  1. Transfer data from Attributes to Geometry
    By Inukshuk in forum AutoCAD Tips & Tricks
    Replies: 0
    Last Post: 2011-11-03, 08:23 AM
  2. Attributes don't move...
    By joeswantek in forum Dynamic Blocks - Technical
    Replies: 4
    Last Post: 2007-07-10, 01:24 PM
  3. Replies: 16
    Last Post: 2006-07-25, 03:44 PM
  4. Can't move geometry from point to coord
    By jgratton in forum AutoCAD General
    Replies: 2
    Last Post: 2006-01-03, 07:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •