user5052625
user5052625

Reputation:

How to apply a transformation for a group of entities inside a polyline?

I have a group of entity objects inside a polyline entity that I want to rescale. I have created Extents3d object to rescale the objects in order to avoid rescale objects one by one, but it doesn't work:

Document document = Application.DocumentManager.MdiActiveDocument;
Database database = document.Database;


using(Transaction transaction = database.TransactionManager.StartTransaction())
{
   BlockTable blockTable = transaction.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable;
   BlockTableRecord blockTableRecord = transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
   Polyline polyline = new Polyline();
   polyline.AddVertexAt(0, new Point2d(0.0, 0.0), 0, 0, 0);
   polyline.AddVertexAt(1, new Point2d(100.0, 100.0), 0, 0, 0);
   polyline.AddVertexAt(2, new Point2d(50.0, 500.0), 0, 0, 0);
   polyline.Closed = true;
   blockTableRecord.AppendEntity(polyline);
   transaction.AddNewlyCreatedDBObject(polyline, true);
   Extents3d boundary = polyline.GeometricExtents;
   boundary.TransformBy(Matrix3d.Scaling(1, Point3d.Origin));
   transaction.commit();
}

Upvotes: 2

Views: 1124

Answers (2)

Augusto Goncalves
Augusto Goncalves

Reputation: 8574

Re-reading your question, if you want to scale entities delimited by the Polygon area, then you first need to select them, the scale. You code is definitely not doing this (neither my suggestion).

To select, use the code described here and copied below: see the special Editor.Select**** method.

After you select, then you can apply the transform. Please observe the origin/base point and scale factor.

[CommandMethod("SEL")]
public void MySelection()
{
    Document doc = Autodesk.AutoCAD
        .ApplicationServices.Application
        .DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;

    Point3d p1 = new Point3d(10.0, 10.0, 0.0);
    Point3d p2 = new Point3d(10.0, 11.0, 0.0);
    Point3d p3 = new Point3d(11.0, 11.0, 0.0);
    Point3d p4 = new Point3d(11.0, 10.0, 0.0);

    Point3dCollection pntCol =
                      new Point3dCollection();
    pntCol.Add(p1);
    pntCol.Add(p2);
    pntCol.Add(p3);
    pntCol.Add(p4);

    int numOfEntsFound = 0;

    PromptSelectionResult pmtSelRes = null;

    TypedValue[] typedVal = new TypedValue[1];
    typedVal[0] = new TypedValue
                 ((int)DxfCode.Start, "Line");

    SelectionFilter selFilter =
              new SelectionFilter(typedVal);
    pmtSelRes = ed.SelectCrossingPolygon
                         (pntCol, selFilter);
    // May not find entities in the UCS area
    // between p1 and p3 if not PLAN view
    // pmtSelRes =
    //    ed.SelectCrossingWindow(p1, p3, selFilter);

    if (pmtSelRes.Status == PromptStatus.OK)
    {
        foreach (ObjectId objId in
            pmtSelRes.Value.GetObjectIds())
        {
            numOfEntsFound++;
        }
        ed.WriteMessage("Entities found " +
                    numOfEntsFound.ToString());
    }
    else
        ed.WriteMessage("\nDid not find entities");
}

Upvotes: 2

Augusto Goncalves
Augusto Goncalves

Reputation: 8574

You should apply the transform to the polyline itself, not the boundary (that represents a geometry space, not an entity).

About scaling in one direction, the problem is the Origin/Base point on the transform: if you set the Origin, it will scale from 0,0,0, now if you want to 'scale in all directions', use a point at the middle of the polyline. Check below.

Also note your scale factor is 1, you need a different value: >1 will is scale up, <1 will scale down.

Document document = Application.DocumentManager.MdiActiveDocument;
Database database = document.Database;

using(Transaction transaction = database.TransactionManager.StartTransaction())
{
   BlockTable blockTable = transaction.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable;
   BlockTableRecord blockTableRecord = transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
   Polyline polyline = new Polyline();
   polyline.AddVertexAt(0, new Point2d(0.0, 0.0), 0, 0, 0);
   polyline.AddVertexAt(1, new Point2d(100.0, 100.0), 0, 0, 0);
   polyline.AddVertexAt(2, new Point2d(50.0, 500.0), 0, 0, 0);
   polyline.Closed = true;
   blockTableRecord.AppendEntity(polyline);
   transaction.AddNewlyCreatedDBObject(polyline, true);
   Extents3d boundary = polyline.GeometricExtents;
   Point3d center = (new LineSegment3d(boundary.MinPoint, boundary.MaxPoint)).MidPoint;
   polyline.TransformBy(Matrix3d.Scaling(1, center));
   transaction.commit();
}

Upvotes: 1

Related Questions