c#,Autocad,显示图案填充的属性



我正在使用c#编写一个AutoCAD插件,需要在填充线对象的中间(例如,圆的中心(显示填充线的属性。我在代码方面有两个问题:

  1. 如何访问圆心?

  2. 如何获取舱口的属性?

我在autocad。

acText.TextString = hatch.Area.ToString(); // Area of hatch

以下是主要取自Stackflow的代码。

[CommandMethod("DisplyArea")]
public static void SelectCirclesToHatch()
{
var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
var filter = new SelectionFilter(new[] { new TypedValue(0, "CIRCLE") });
var selection = ed.GetSelection(filter);
int vr = 1;
if (selection.Status != PromptStatus.OK)
return;
using (var tr = db.TransactionManager.StartTransaction())
{
var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
foreach (var id in selection.Value.GetObjectIds())
{
var ids = new ObjectIdCollection(new[] { id });
using (var hatch = new Hatch())
{
curSpace.AppendEntity(hatch);
tr.AddNewlyCreatedDBObject(hatch, true);
hatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
hatch.Associative = true;
hatch.AppendLoop(HatchLoopTypes.Outermost, ids);
hatch.EvaluateHatch(true);
DBText acText = new DBText();

//它需要是CIRCLE.CENTER,但我如何访问它??????

acText.Position = new Point3d(2,2,0);     

acText.TextString = hatch.Area.ToString(); // Area of hatch
acText.Height = 0.5;
curSpace.AppendEntity(acText);
tr.AddNewlyCreatedDBObject(acText, true);
}
}
tr.Commit();
}
}

有什么解决方案吗?

  1. 如何访问圆心
    答案:
    Circle oCircle = tr.GetObject(id, OpenMode.ForRead) as Circle; var _centerPosition = oCircle.Center;

  2. 如何获取舱口的属性
    答案:
    您需要将
    hatch.AppendLoop(HatchLoopTypes.Outermost, ids);
    更改为hatch.AppendLoop(HatchLoopTypes.External, ids);

我更改了你的代码,它对我有效

public static void SelectCirclesToHatch()
{
var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
var filter = new SelectionFilter(new[] { new TypedValue(0, "CIRCLE") });
var selection = ed.GetSelection(filter);
int vr = 1;
if (selection.Status != PromptStatus.OK) { return; }
using (var tr = db.TransactionManager.StartTransaction())
{
var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
foreach (var id in selection.Value.GetObjectIds())
{
try
{
var ids = new ObjectIdCollection(new[] { id });
using (var hatch = new Hatch())
{
curSpace.AppendEntity(hatch);
tr.AddNewlyCreatedDBObject(hatch, true);
hatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
hatch.Associative = true;
hatch.AppendLoop(HatchLoopTypes.External, ids);
hatch.EvaluateHatch(true);
hatch.HatchStyle = HatchStyle.Normal;
Circle oCircle = tr.GetObject(id, OpenMode.ForRead) as Circle;
var _centerPosition = oCircle.Center;
DBText acText = new DBText();
acText.Position = _centerPosition;
acText.TextString = hatch.Area.ToString(); // Area of hatch
acText.Height = 0.5;
curSpace.AppendEntity(acText);
tr.AddNewlyCreatedDBObject(acText, true);
}
}
catch { }
}
tr.Commit();
}
}

您可以通过将Id强制转换为如下实体来轻松获取圆心:

Circle c = tr.GetObject(id, OpenMode.ForRead) as Circle;
acText.Position = c.Center;

关于舱口区域的问题,我认为首先你需要定义舱口

using (var hatch = new Hatch())
{

hatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
hatch.Associative = true;
hatch.AppendLoop(HatchLoopTypes.External, ids);
hatch.EvaluateHatch(true);

然后将其添加到空格中;

curSpace.AppendEntity(hatch);
tr.AddNewlyCreatedDBObject(hatch, true);

最后读取区域

acText.TextString = hatch.Area.ToString()

首先将图案填充添加到空间中,但它还没有几何图形。

[CommandMethod("DisplyArea")]
public static void SelectCirclesToHatch()
{
var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
var filter = new SelectionFilter(new[] { new TypedValue(0, "CIRCLE") });
var selection = ed.GetSelection(filter);
if (selection.Status != PromptStatus.OK)
return;
using (var tr = db.TransactionManager.StartTransaction())
{
var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
foreach (var id in selection.Value.GetObjectIds())
{
Entity acEnt = tr.GetObject(id, OpenMode.ForRead) as Entity;
Circle acCir = acEnt as Circle;
Point3d cntrPnt = acCir.Center;
// Open the Block table for read
BlockTable acBlkTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec = tr.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
// Create the hatch object and append it to the block table record
using (Hatch acHatch = new Hatch())
{
acBlkTblRec.AppendEntity(acHatch);
tr.AddNewlyCreatedDBObject(acHatch, true);
ObjectIdCollection ids = new ObjectIdCollection();
ids.Add(id);
// Set the properties of the hatch object
// Associative must be set after the hatch object is appended to the 
// block table record and before AppendLoop
acHatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
acHatch.Associative = true;
acHatch.AppendLoop(HatchLoopTypes.Outermost, ids);
acHatch.EvaluateHatch(true);
acHatch.HatchStyle = HatchStyle.Outer;
ObjectId hatchId = acHatch.ObjectId;
}
}
}
}

最新更新