.NET CAD二次开发学习第一天

基于浩辰CAD2019

需求:

开发线转圆简单命令。
命令过程:
1) 请选择图中直线(要求支持一次选多个):
2) 弹出对话框,输入圆的图层名和半径
3) 点对话框中确定按钮,结束命令。
命令执行效果:
所选每条直线的起点和终点处,自动生成两个圆;同时,所有直线自动整体平移MOVE一个向量AcGeVector3d(1000,1000,0)。

代码如下

using System;
using GrxCAD.Runtime;
using GrxCAD.ApplicationServices;
using GrxCAD.EditorInput;
using GrxCAD.Geometry;
using GrxCAD.DatabaseServices;
using GrxCAD.Windows;
//using System.Windows.Forms;

namespace ArxFirstTest
{
public class Class1
{


[CommandMethod("LineToCircle")]
public void LinetoCircle()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor editor = doc.Editor;
Database db = doc.Database;
SelectionSet acSSet = null;
Form1 form = new Form1();
PromptSelectionOptions pso = new PromptSelectionOptions()
{
AllowDuplicates = false,
SelectEverythingInAperture = true,
SingleOnly = false,
RejectObjectsFromNonCurrentSpace = false,
//Keywords = { "0","1"},

};
PromptSelectionResult ssPsr = editor.GetSelection(pso);
if (ssPsr.Status != PromptStatus.OK)
{
return;
}
else
{
acSSet = ssPsr.Value;
}
using (Transaction transaction = db.TransactionManager.StartTransaction())
{
form.ShowDialog();
foreach (SelectedObject acSSobj in acSSet)
{
if (acSSobj!=null)
{
Entity entity = transaction.GetObject(acSSobj.ObjectId, OpenMode.ForWrite) as Entity;
if (entity is Line)
{

string s = form.comboBox1.Text;
double d = Double.Parse(form.textBox2.Text);
Line line = (Line)entity;
Circle cir1 = new Circle();
cir1.Center = line.StartPoint;
cir1.Radius = d;
cir1.Layer = s;
Circle cir2 = new Circle();
cir2.Center = line.EndPoint;
cir2.Radius = d;
cir2.Layer = s;
line.TransformBy(Matrix3d.Displacement(new Vector3d(1000, 1000, 0)));
ObjectId idModelSpace = SymbolUtilityServices.GetBlockModelSpaceId(db);
BlockTableRecord btr = transaction.GetObject(idModelSpace, OpenMode.ForWrite) as BlockTableRecord;
btr.AppendEntity(line);
btr.AppendEntity(cir1);
btr.AppendEntity(cir2);
transaction.AddNewlyCreatedDBObject(line, true);
transaction.AddNewlyCreatedDBObject(cir1, true);
transaction.AddNewlyCreatedDBObject(cir2, true);

}
else
{
Application.ShowAlertDialog("选择的实体中有为非直线,请重新选择!");
return;
}

}

}

transaction.Commit();
}


}
}
}

 

using System;
using GrxCAD.Runtime;
using GrxCAD.ApplicationServices;
using GrxCAD.EditorInput;
using GrxCAD.Geometry;
using GrxCAD.DatabaseServices;
using System.Collections;
//using System.Windows.Forms;

namespace ArxFirstTest
{
public partial class Form1 : System.Windows.Forms.Form
{

public Form1()
{


InitializeComponent();
}

public void textBox1_TextChanged(object sender, EventArgs e)
{

}

public void Form1_Load(object sender, EventArgs e)
{
using (Database db = HostApplicationServices.WorkingDatabase)
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
using (LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead))
{
foreach (ObjectId id in lt)
{
LayerTableRecord ltr = (LayerTableRecord)trans.GetObject(id, OpenMode.ForRead);
comboBox1.Items.Add(ltr.Name);
}
}
trans.Commit();
}
}
}

private void textBox2_TextChanged(object sender, EventArgs e)
{

}

public void button1_Click(object sender, EventArgs e)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor editor = doc.Editor;
if (Double.TryParse(textBox2.Text, out double b))
{
//editor.Command("Enter");
this.Close();
}
else
{
Application.ShowAlertDialog("数据输入有误,请重新输入");
}
}

public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}
}
}

posted on 2019-04-09 16:01 yannis.wh,yang 阅读(...) 评论(...) 编辑 收藏

导航

公告

统计