.NET CAD二次开发学习 直线画矩形并转换成组

时间:2022-11-06 05:05:39

主要代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GrxCAD.Runtime;
using GrxCAD.ApplicationServices;
using GrxCAD.EditorInput;
using GrxCAD.Geometry;
using GrxCAD.DatabaseServices;
using GrxCAD.Windows;

namespace ArxFirstTest
{
public class XRect
{

private Point3d StartPoint;
private Point3d EndPoint;
private double width;

public void XrecPic()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor editor = doc.Editor;
Database db = doc.Database;
PromptPointOptions promptPoint1 = new PromptPointOptions("\n请指定起点<退出>");
PromptPointResult pointResult1 = editor.GetPoint(promptPoint1);
PromptPointOptions promptPoint2 = new PromptPointOptions("\n请指定终点<退出>");
PromptPointResult pointResult2 = editor.GetPoint(promptPoint2);
PromptDoubleOptions pDoubleOptions = new PromptDoubleOptions("\n请输入宽度<退出>");
PromptDoubleResult pDoubleRes = editor.GetDouble(pDoubleOptions);
if (pointResult1.Status == PromptStatus.OK && pointResult2.Status == PromptStatus.OK && pDoubleRes.Value > 0)
{
using (Transaction trans = doc.TransactionManager.StartTransaction())
{
StartPoint = pointResult1.Value;
EndPoint = pointResult2.Value;
width = pDoubleRes.Value;
Line line = new Line();
line.StartPoint = StartPoint;
line.EndPoint = EndPoint;
DBObjectCollection dboj = line.GetOffsetCurves(width / 2);
DBObjectCollection dboj1 = line.GetOffsetCurves(width / 2 - width);
Line line1 = (Line)dboj[0];
Line line2 = (Line)dboj1[0];
Line line3 = new Line();
Line line4 = new Line();
line3.StartPoint = line1.StartPoint;
line3.EndPoint = line2.StartPoint;
line4.StartPoint = line1.EndPoint;
line4.EndPoint = line2.EndPoint;
ObjectId idModelSpace = SymbolUtilityServices.GetBlockModelSpaceId(db);
BlockTableRecord btr = trans.GetObject(idModelSpace, OpenMode.ForWrite) as BlockTableRecord;
btr.AppendEntity(line1);
btr.AppendEntity(line2);
btr.AppendEntity(line3);
btr.AppendEntity(line4);
trans.AddNewlyCreatedDBObject(line1, true);
trans.AddNewlyCreatedDBObject(line2, true);
trans.AddNewlyCreatedDBObject(line3, true);
trans.AddNewlyCreatedDBObject(line4, true);
Entity ent1 = (Entity)line1;
Entity ent2 = (Entity)line2;
Entity ent3 = (Entity)line3;
Entity ent4 = (Entity)line4;
Group group = new Group();
group.Append(ent1.ObjectId);
group.Append(ent2.ObjectId);
group.Append(ent3.ObjectId);
group.Append(ent4.ObjectId);
DBDictionary groupdic = trans.GetObject(db.GroupDictionaryId,OpenMode.ForWrite) as DBDictionary;
group.Selectable = true;
groupdic.SetAt(group.Name, group);
trans.AddNewlyCreatedDBObject(group, true);
trans.Commit();

}

}
else
{
Application.ShowAlertDialog("选择或者输入有误,请重新输入");
return;

}

}
}
}