您的位置:首页 > 服装鞋帽 > 女装 > 利用C++Builder通过AutoCAD ActiveX进行AutoCAD二次开发(三)

利用C++Builder通过AutoCAD ActiveX进行AutoCAD二次开发(三)

luyued 发布于 2011-06-18 09:13   浏览 N 次  

第六步 绘制基本图元

效果:

void __fastcall TMainForm::DrawHoleActionExecute(TObject *Sender)

{

THoleForm* HoleForm = new THoleForm(this);

if(HoleForm->ShowModal() == mrOk)

{

Variant APoint = PickPoint();

if(HoleForm->RadioGroup->ItemIndex == 0) //圆孔

{

double d = StrToFloat(HoleForm->StrGrid->Cells[1][1]);

Hole1(APoint.GetElement(0),APoint.GetElement(1),d,acsScale);

}

else if(HoleForm->RadioGroup->ItemIndex == 1) //长孔

{

float d = StrToFloat(HoleForm->StrGrid->Cells[1][1]);

float l = StrToFloat(HoleForm->StrGrid->Cells[1][2]);

Hole2(APoint.GetElement(0),APoint.GetElement(1),d,l,acsScale);

}

else //螺纹孔

{

float d = StrToFloat(HoleForm->StrGrid->Cells[1][1]);

float t = StrToFloat(HoleForm->StrGrid->Cells[1][2]);

Hole3(APoint.GetElement(0),APoint.GetElement(1),d,t,acsScale);

}

}

delete HoleForm;

}

void __fastcall Hole1(double x,double y,double d,double s)

{

double dc = d*s;

double dl = 1.2f*d*s;

Circle(x,y,dc);

CenterLine(x,y,dl,dl);

}

void __fastcall Hole2(double x,double y,double d,double l,double s)

{

double r = d*s/2.0f;

double b = l*s;

Arc(x-b/2.0f,y,r,90,270);

Arc(x+b/2.0f,y,r,270,90);

Line(x-b/2.0f,y+r,x+b/2.0f,y+r);

Line(x-b/2.0f,y-r,x+b/2.0f,y-r);

CenterLine(x,y,b+1.2f*2.0f*r,1.3f*2.0f*r);

Line(x-b/2.0f,y-1.2f*r,x-b/2.0f,y+1.2f*r,acsLayer3);

Line(x+b/2.0f,y-1.2f*r,x+b/2.0f,y+1.2f*r,acsLayer3);

}

void __fastcall Hole3(double x,double y,double d,double t,double s)

{

double dc = (d-1.6f*t)*s;

double dl = 1.2f*d*s;

double r = d*s/2.0f;

Circle(x,y,dc);

Arc(x,y,r,180,90,acsLayer1);

CenterLine(x,y,dl,dl);

}

//~~~~~~~~

//基本图元

//~~~~~~~~

AcadLinePtr __fastcall Line(double sx,double sy,double ex,double ey,UnicodeString LayerName)

{

Variant StartPoint = VariantPoint(sx,sy);

Variant EndPoint = VariantPoint(ex,ey);

AcadLinePtr ALine;

ACADApp->ActiveDocument->ModelSpace->AddLine(StartPoint,EndPoint,&ALine);

if(LayerName.IsEmpty() == false)

ALine->Layer = FindLayer(LayerName);

ALine->LinetypeScale = acsLineTypeScale;

return ALine;

}

void __fastcall CenterLine(double x,double y,double w,double h)

{

Line(x-w/2.0f,y,x+w/2.0f,y,acsLayer3);

Line(x,y-h/2.0f,x,y+h/2.0f,acsLayer3);

}

AcadCirclePtr __fastcall Circle(double x,double y,double d,UnicodeString LayerName)

{

Variant ACenter = VariantPoint(x,y);

AcadCirclePtr ACircle;

ACADApp->ActiveDocument->ModelSpace->AddCircle(ACenter,d/2,&ACircle);

if(LayerName.IsEmpty() == false)

ACircle->Layer = FindLayer(LayerName);

ACircle->LinetypeScale = acsLineTypeScale;

return ACircle;

}

AcadArcPtr __fastcall Arc(double x,double y,double r,double sa,double ea,UnicodeString LayerName)

{

Variant ACenter = VariantPoint(x,y);

AcadArcPtr AArc;

ACADApp->ActiveDocument->ModelSpace->AddArc(ACenter,r,AngToRad(sa),AngToRad(ea),&AArc);

if(LayerName.IsEmpty() == false)

AArc->Layer = FindLayer(LayerName);

AArc->LinetypeScale = acsLineTypeScale;

return AArc;

}

AcadPolylinePtr __fastcall Polyline(double* x,double* y ,int n,UnicodeString LayerName)

{

Variant Points = VariantPoints(x,y,n);

AcadPolylinePtr APolyline;

ACADApp->ActiveDocument->ModelSpace->AddPolyline(Points,&APolyline);

if(LayerName.IsEmpty() == false)

APolyline->Layer = FindLayer(LayerName);

APolyline->LinetypeScale = acsLineTypeScale;

return APolyline;

}

AcadPolylinePtr __fastcall Rectangle(double x,double y ,double w,double h,UnicodeString LayerName)

{

double XCoord[5];

double YCoord[5];

XCoord[0] = x-w/2.0f;

YCoord[0] = y-h/2.0f;

XCoord[1] = x+w/2.0f;

YCoord[1] = y-h/2.0f;

XCoord[2] = x+w/2.0f;

YCoord[2] = y+h/2.0f;

XCoord[3] = x-w/2.0f;

YCoord[3] = y+h/2.0f;

XCoord[4] = x-w/2.0f;

YCoord[4] = y-h/2.0f;

return Polyline(XCoord,YCoord,5,LayerName);

}

void __fastcall RoundRect(double x,double y ,double w,double h,double r,UnicodeString LayerName)

{

Line(x-w/2.0f,y-h/2.0f+r,x-w/2.0f,y+h/2.0f-r,LayerName);

Line(x+w/2.0f,y-h/2.0f+r,x+w/2.0f,y+h/2.0f-r,LayerName);

Line(x-w/2.0f+r,y+h/2.0f,x+w/2.0f-r,y+h/2.0f,LayerName);

Line(x-w/2.0f+r,y-h/2.0f,x+w/2.0f-r,y-h/2.0f,LayerName);

Arc(x+w/2.0f-r,y+h/2.0f-r,r,0,90,LayerName);

Arc(x-w/2.0f+r,y+h/2.0f-r,r,90,180,LayerName);

Arc(x-w/2.0f+r,y-h/2.0f+r,r,180,270,LayerName);

Arc(x+w/2.0f-r,y-h/2.0f+r,r,270,360,LayerName);

}

AcadTextPtr __fastcall DText(double x,double y,UnicodeString AStr,double h,UnicodeString LayerName)

{

Variant InsertPoint = VariantPoint(x,y);

Variant ATextStr = AStr;

AcadTextPtr AText;

ACADApp->ActiveDocument->ModelSpace->AddText(ATextStr,InsertPoint,h,&AText);

if(LayerName.IsEmpty() == false)

AText->Layer = FindLayer(LayerName);

AText->StyleName = FindTextStyle(acsTextStyle);

return AText;

}

AcadMTextPtr __fastcall MText(double x,double y,double w,double h,UnicodeString AStr,double th,int TextAlign,UnicodeString LayerName)

{

Variant InsertPoint = VariantPoint(x,y);

Variant ATextStr = AStr;

AcadMTextPtr AText;

ACADApp->ActiveDocument->ModelSpace->AddMText(InsertPoint,w,ATextStr,&AText);

if(LayerName.IsEmpty() == false)

AText->Layer = FindLayer(LayerName);

AText->StyleName = FindTextStyle(acsTextStyle);

AText->AttachmentPoint = AcAttachmentPoint(TextAlign);

AText->Height = th;

AText->set_InsertionPoint(InsertPoint);

return AText;

}

Variant __fastcall FindLayer(UnicodeString ALayerName)

{

bool Find = false;

UnicodeString AStr;

for(int i=0;iActiveDocument->Layers->Count;i++)

{

AcadLayerPtr ALayer;

ACADApp->ActiveDocument->Layers->Item((Variant)i,&ALayer);

UnicodeString LayerName = ALayer->Name;

if(LayerName==ALayerName)

{

Find = true;

AStr = LayerName;

break;

}

}

if(Find == false)

{

Variant tmp = ALayerName;

AcadLayerPtr ALayer;

ACADApp->ActiveDocument->Layers->Add(tmp,&ALayer);

if(tmp == acsLayer1) //细实线

ALayer->color = AcColor(2);

else if(tmp == acsLayer2) //虚线

{

ALayer->color = AcColor(4);

ALayer->Linetype = FindLineType(acsLineType1);

}

else if(tmp == acsLayer3) //点划线

{

ALayer->color = AcColor(1);

ALayer->Linetype = FindLineType(acsLineType2);

}

else if(tmp == acsLayer4) //双点划线

{

ALayer->color = AcColor(5);

ALayer->Linetype = FindLineType(acsLineType3);

}

else if(tmp == acsLayer5) //尺寸

ALayer->color = AcColor(3);

else if(tmp == acsLayer6) //剖面线

ALayer->color = AcColor(6);

AStr = ALayer->Name;

}

return AStr;

}

Variant __fastcall FindLineType(UnicodeString ALineTypeName)

{

bool Find = false;

UnicodeString AStr;

for(int i=0;iActiveDocument->Linetypes->Count;i++)

{

AcadLineTypePtr ALineType;

ACADApp->ActiveDocument->Linetypes->Item((Variant)i,&ALineType);

Variant LineTypeName = ALineType->Name;

if( LineTypeName == (Variant)ALineTypeName)

{

Find = true;

AStr = LineTypeName;

break;

}

}

if(Find == false)

{

AcadLineTypePtr ALineType;

Variant LineTypeName = ALineTypeName;

Variant LineTypeFileName = "ACADISO.Lin";

ACADApp->ActiveDocument->Linetypes->Load(LineTypeName,LineTypeFileName);

AStr = LineTypeName;

}

return AStr;

}

Variant __fastcall FindTextStyle(UnicodeString AStyleName)

{

bool Find = false;

UnicodeString AStr;

for(int i=0;iActiveDocument->TextStyles->Count;i++)

{

AcadTextStylePtr ATextStyle;

ACADApp->ActiveDocument->TextStyles->Item((Variant)i,&ATextStyle);

Variant StyleName = ATextStyle->Name;

Variant FontFile = ATextStyle->fontFile;

if( StyleName == (Variant)AStyleName)

{

Find = true;

AStr = StyleName;

break;

}

}

if(Find == false)

{

AcadTextStylePtr ATextStyle;

Variant TextStyleName = AStyleName;

Variant FontFileName = GetCurrentDir()+"\\simfang.ttf";

ACADApp->ActiveDocument->TextStyles->Add(TextStyleName,&ATextStyle);

ATextStyle->fontFile = FontFileName;

ATextStyle->Height = 3.5f;

ATextStyle->ObliqueAngle = AngToRad(5);

// ATextStyle-> //宽高比没法设置

AStr = ATextStyle->Name;

}

return AStr;

}


MSN空间完美搬家到新浪博客!

图文资讯
广告赞助商