实现思路:实例化对应图像类,并调用AddEntity方法添加到DxfDocument对象中,最后保存文件。
1、绘制多段线
LwPolyline widthLine = new LwPolyline();//新建LwPolyline对象
LwPolylineVertex startVertex = new LwPolylineVertex(new Vector2(0, 0));//新建坐标点1
LwPolylineVertex endVertex = new LwPolylineVertex(new Vector2(10, 10));//新建坐标点2
widthLine.Vertexes.AddRange(new[] {startVertex, endVertex});//给多段线增加坐标点
LwPolyline poly = new LwPolyline();
poly.Vertexes.Add(new LwPolylineVertex(-10, -10));
poly.Vertexes.Add(new LwPolylineVertex(10, -10));
poly.Vertexes.Add(new LwPolylineVertex(10, 10));
poly.Vertexes.Add(new LwPolylineVertex(-10, 10));
poly.Vertexes[2].Bulge = 1;
poly.IsClosed = true;//封闭图形
2、绘制圆
Circle circle = new Circle {Center = new Vector3(0,0,10), Radius = 2};//圆心坐标、半径
3、绘制弧线
Arc arc = new Arc {Center = new Vector3(0,0,10), Radius = 2, StartAngle = 30, EndAngle = 160};//圆心坐标、半径、起始角度、结束角度
4、填充
private static void ComplexHatch()
{
//设置填充样式
HatchPattern pattern = HatchPattern.Load("hatch\\acad.pat", "ESCHER");
pattern.Scale = 1.5;
pattern.Angle = 30;
LwPolyline poly = new LwPolyline();
poly.Vertexes.Add(new LwPolylineVertex(-10, -10));
poly.Vertexes.Add(new LwPolylineVertex(10, -10));
poly.Vertexes.Add(new LwPolylineVertex(10, 10));
poly.Vertexes.Add(new LwPolylineVertex(-10, 10));
poly.IsClosed = true;//图形需要封闭
//给封闭图形填充
List<HatchBoundaryPath> boundary = new List<HatchBoundaryPath>
{
new HatchBoundaryPath(new List<EntityObject> {poly})
};
Hatch hatch = new Hatch(pattern, boundary, true);
DxfDocument dxf = new DxfDocument();
dxf.AddEntity(poly);//添加图形
dxf.AddEntity(hatch);//添加填充
dxf.Save("complexhatch.dxf");
DxfDocument dxf2 = DxfDocument.Load("complexhatch.dxf");
dxf2.Save("complexhatch2.dxf");
}
5、块
Block block = new Block("MyBlock");
block.Entities.Add(poly);
Insert insert = new Insert(block);
insert.Position = new Vector3(12,22,4);
insert.Scale = new Vector3(1.25,0.75,1);
insert.Scale = new Vector3(1,2,3);
insert.Rotation = 30;
insert.Normal = new Vector3(1,1,1);
DxfDocument doc = new DxfDocument();
doc.AddEntity(poly);
doc.AddEntity(insert);
List<EntityObject> entities = insert.Explode();
doc.AddEntity(entities);
doc.Save("test.dxf");
Block block = Block.Load(baseBlk.Name + ".dxf", "MyBlock");//从文件中读取块
使用教程:
4、绘制图形