如何对折线内的一组实体应用转换?
How to apply a transformation for a group of entities inside a polyline?
我在 polyline entity
中有一组 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();
}
您应该将变换应用到折线本身,而不是边界(表示几何 space,而不是实体)。
关于在一个方向上缩放,问题是变换上的 Origin/Base 点:如果你设置原点,它将从 0,0,0 开始缩放,现在如果你想 'scale in all directions',在多段线的中间使用一个点。检查下面。
另请注意,您的比例因子是 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();
}
重新阅读你的问题,如果你想缩放由多边形区域分隔的实体,那么你首先需要 select 它们,缩放。你的代码绝对不是这样做的(我的建议也不是)。
到select,使用代码described here并复制如下:见特殊Editor.Select****方法。
在 select 之后,您就可以应用转换了。请注意 origin/base 点和比例因子。
[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");
}
我在 polyline entity
中有一组 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();
}
您应该将变换应用到折线本身,而不是边界(表示几何 space,而不是实体)。
关于在一个方向上缩放,问题是变换上的 Origin/Base 点:如果你设置原点,它将从 0,0,0 开始缩放,现在如果你想 'scale in all directions',在多段线的中间使用一个点。检查下面。
另请注意,您的比例因子是 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();
}
重新阅读你的问题,如果你想缩放由多边形区域分隔的实体,那么你首先需要 select 它们,缩放。你的代码绝对不是这样做的(我的建议也不是)。
到select,使用代码described here并复制如下:见特殊Editor.Select****方法。
在 select 之后,您就可以应用转换了。请注意 origin/base 点和比例因子。
[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");
}