获取到一个图形对象 : AcadDocument oAcadDocument = oAcadApplication.Documents.Open(FileName, null, null);
有了图形对象,我们就可以给图形对象添加跟着线条图形
double[] StartPoint = new double[3] { 10, 10, 0 };
double[] EndPoint = new double[3] { 50, 50, 0 };
1 画一条直线
AcadLine oAcadLine = oAcadDocument.ModelSpace.AddLine(StartPoint , EndPoint );
如果是虚线,则设置属性值 oAcadLine .LinetypeScale = 0.5;
可以给线条设置一下属性,如颜色:oAcadLine .color = ACAD_COLOR.acRed;
2 画一个圆 :传入圆心坐标和半径长度:double radius=10;
oAcadDocument.ModelSpace.AddCircle(EndPoint , radius);
3 画弧线,有时候还需要话一条弧线 ,画弧度,使用π表示弧度
var ARC = oAcadDocument.ModelSpace.AddArc(EndPoint , radius, Math.PI / 2, Math.PI * 1.5);
4 添加文字说明 double fontsize=5;
oAcadDocument.ModelSpace.AddText("玻璃", EndPoint , fontsize);
基本的线条描绘已经知道了,就可以组合画其他图形。
5 画一个方框
public static void CoordinatePointAssignment(ref double[] CoordinatePoint,double x,double y,double z)
{
CoordinatePoint[0] = x;
CoordinatePoint[1] = y;
CoordinatePoint[2] = z;
}
//画方框,得知道四个角的坐标
public static void DrawRectangleXX(ref AcadDocument oAcadDocument, double[] StartPoint, double Width, double Height")
{
//--- Start_Point 起始坐标点
double[] OnePoint = new double[PointArray]; double[] TwoPoint = new double[PointArray];
double[] ThreePoint = new double[PointArray]; double[] FourPoint = new double[PointArray];
double x, y;
x = StartPoint[0]; y = StartPoint[1];
//起始点是 左上角的 点
CoordinatePointAssignment(ref OnePoint, x, y, 0);
CoordinatePointAssignment(ref TwoPoint, x + Width, y, 0);
CoordinatePointAssignment(ref ThreePoint, x + Width, y - Height, 0);
CoordinatePointAssignment(ref FourPoint, x, y - Height, 0);
//有了坐标--最后绘制连线,就变成了方框
AcadLine oAcadLine;
oAcadLine = oAcadDocument.ModelSpace.AddLine(OnePoint, TwoPoint);
oAcadLine = oAcadDocument.ModelSpace.AddLine(TwoPoint, ThreePoint);
oAcadLine = oAcadDocument.ModelSpace.AddLine(ThreePoint, FourPoint);
oAcadLine = oAcadDocument.ModelSpace.AddLine(FourPoint, OnePoint);
}
6 加一个标注说明:如下图,标注一下线条的宽度754。
///
/// 文本坐标点
///
/// 输出结果
/// 起始点
/// 终点
/// 值类别
/// 偏移值
public static void TextCoordinatePoint(ref double[] TaggedCoordinates,double[] StartPoint, double[] EndPoin,int TypeValue, double Deviate)
{
if (TypeValue == 1)
{
//---横向向的标注
TaggedCoordinates[0] = (StartPoint[0] + EndPoin[0]) / 2;
TaggedCoordinates[1] = StartPoint[1] + Deviate;
TaggedCoordinates[2] = StartPoint[2];
}
else
{
//---纵向的标注
TaggedCoordinates[0] = StartPoint[0] + Deviate;
TaggedCoordinates[1] = (StartPoint[1] + EndPoin[1]) / 2;
TaggedCoordinates[2] = StartPoint[2];
}
}
///
/// 添加标注
///
/// 文档对象
/// 开始位置
/// 结束位置
/// 1位横向标注
/// 正负值位为开口方向
/// 标注内容
public static void Addtext(ref AcadDocument oAcadDocument, double[] StartPoint, double[] EndPoint, int TypeValue, double Deviate,string text,int textH=0)
{
AcadDimAligned oAcadDimAligned; double[] uTemporaryPoint = new double[3];
TextCoordinatePoint(ref uTemporaryPoint, StartPoint, EndPoint, TypeValue, Deviate);
oAcadDimAligned = oAcadDocument.ModelSpace.AddDimAligned(StartPoint, EndPoint, uTemporaryPoint);
oAcadDimAligned.TextOverride = text;if(textH>0) oAcadDimAligned.TextHeight = textH;
}
最后使用: Addtext(ref oAcadDocument, StartP, EndP, 1, -10, "754");
线条的开始坐标 StartP ,结束坐标 EndP,1 方法里表示横向标注,-10表示坐标向下距离10个像素,画双箭头标注754
CAD绘图,无非就是在图形对象里画线画框画圆,只要知道坐标开始,坐标画到哪里了,再在此位置加入我们要画的对应图形就行了,下面是我完成的一张比较简单的出图: