如何为折线内的一组实体应用变换?

时间:2015-09-23 14:05:49

标签: c# autocad-plugin

我在entity内有一组polyline entity个对象,我想要重新缩放。我创建了Extents3d对象来重新缩放对象,以避免逐个重新缩放对象,但它不起作用:

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();
}

2 个答案:

答案 0 :(得分:2)

重新阅读您的问题,如果您想缩放由多边形区域分隔的实体,那么您首先需要选择它们,即比例。你的代码绝对没有这样做(我的建议都没有)。

要选择,请使用代码described here并复制如下:请参阅特殊的Editor.Select ****方法。

选择后,您可以应用转换。请注意原点/基点和比例因子。

[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");
}

答案 1 :(得分:1)

您应该将变换应用于折线本身,而不是边界(表示几何空间,而不是实体)。

关于在一个方向上缩放,问题是变换上的原点/基点:如果设置原点,它将从0,0,0缩放,现在如果你想在所有方向上缩放& #39;,使用折线中间的一个点。检查下面。

另请注意,您的比例因子为1,您需要一个不同的值:> 1将按比例放大,< 1将按比例缩小。

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();
}