CAD二次开发实验报告
CAD二次实验报告3
CAD开发实验(四)实验三 AutoCAD图形块的创建与应用一、主要内容1、创建块定义及插入块参照2、创建带有属性的块定义及插入带有属性的块参照3、在对话框中查看块定义的图标4、在当前文件中插入外部文件中的块5、在当前文件中插入其他DWG文件二、学时安排:2学时三、预习内容理解块的含义,回顾块和带属性块的定义四、AutoCAD图形块的创建与应用4.1创建块定义及插入块参照4.1.1 创建块定义代码://创建块public Document pDoc =Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;[CommandMethod("CreateBlock")]public void CreateBlock(){ Database acCurDb = pDoc.Database;using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,OpenMode.ForWrite) as BlockTable;// 创建新的块表记录BlockTableRecord acBlkTblRec = new BlockTableRecord();string blkname = "三角点"; = blkname;acBlkTblRec.Origin = new Point3d(0, 0, 0);Point3d pCenter = new Point3d(0, 0, 0);//创建中心点DBPoint pDBPt = new DBPoint(pCenter);//创建圆Circle pCir = new Circle(pCenter, new Vector3d(0, 0, 1), 0.25);//创建三角形Polyline pTri = new Polyline(4);pTri.AddVertexAt(0, new Point2d(0, 1), 0, 0, 0);pTri.AddVertexAt(1, new Point2d(0.866, -0.5), 0, 0, 0);pTri.AddVertexAt(2, new Point2d(-0.866, -0.5), 0, 0, 0);pTri.AddVertexAt(3, new Point2d(0, 1), 0, 0, 0);//给定块的原点acBlkTblRec.Origin = new Point3d(0, 0, 0);//将图形实体添加到块表记录acBlkTblRec.AppendEntity(pDBPt);acBlkTblRec.AppendEntity(pCir);acBlkTblRec.AppendEntity(pTri);//将块记录添加到块表acBlkTbl.Add(acBlkTblRec);acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);// 提交修改并销毁事务mit();}}4.1.2插入卡块参照→代码://插入块[CommandMethod("InsertBlock")]public void InsertBlock(){Database acCurDb = pDoc.Database;// 启动一个事务 Start a transactionusing (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) {BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite) as BlockTable;// 以写方式打开模型空间块表记录BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;//获取三角点块,并创建块引用BlockTableRecord pBlockTableRecord = acTrans.GetObject(acBlkTbl["三角点"], OpenMode.ForRead) as BlockTableRecord;BlockReference bref = new BlockReference(new Point3d(10, 10, 0), pBlockTableRecord.ObjectId);BlockReference bref2 = new BlockReference(new Point3d(30, 30, 0), pBlockTableRecord.ObjectId);acBlkTblRec.AppendEntity(bref2);acTrans.AddNewlyCreatedDBObject(bref2, true);mit();}}4.2创建带有属性的块定义及插入带有属性的块参照4.2.1 创建带有属性的块定义→创建一个名为“三角点”的带属性块的记录,包括以下实体:➢一个点➢一个圆➢一个三角形➢一个名为三角点的属性代码:#region创建带属性的块[CommandMethod("CreateAtrrBlock")]public void CreateAtrrBlock(){ Database acCurDb = pDoc.Database;using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,OpenMode.ForWrite) as BlockTable;BlockTableRecord acBlkTblRec = new BlockTableRecord();string blkname = "三角点"; = blkname;acBlkTblRec.Origin = new Point3d(0, 0, 0);Point3d pCenter = new Point3d(0, 0, 0);//创建中心点DBPoint pDBPt = new DBPoint(pCenter);//创建圆Circle pCir = new Circle(pCenter, new Vector3d(0, 0, 1), 0.25);//创建三角形Polyline pTri = new Polyline(4);pTri.AddVertexAt(0, new Point2d(0, 1), 0, 0, 0);pTri.AddVertexAt(1, new Point2d(0.866, -0.5), 0, 0, 0);pTri.AddVertexAt(2, new Point2d(-0.866, -0.5), 0, 0, 0);pTri.AddVertexAt(3, new Point2d(0, 1), 0, 0, 0);Polyline pPl = new Polyline(2);pPl.AddVertexAt(0, new Point2d(0.9, 0), 0, 0, 0);pPl.AddVertexAt(1, new Point2d(4.5, 0), 0, 0, 0);//定义属性Point3d pPosition = new Point3d(1, 0.2, 0);AttributeDefinition pAtrDef = new AttributeDefinition(pPosition, "洼地", "三角点名", "输入三角点名", acCurDb.Textstyle);pAtrDef.Height = 1;Point3d pPosition2 = new Point3d(1, -1.2, 0);AttributeDefinition pHVaue = new AttributeDefinition(pPosition2, "0.00", "高程值", "输入高程值", acCurDb.Textstyle);pHVaue.Height = 1;//给定块的原点acBlkTblRec.Origin = new Point3d(0, 0, 0);//将图形实体添加到块表记录acBlkTblRec.AppendEntity(pDBPt);acBlkTblRec.AppendEntity(pCir);acBlkTblRec.AppendEntity(pTri);acBlkTblRec.AppendEntity(pPl);acBlkTblRec.AppendEntity(pAtrDef);acBlkTblRec.AppendEntity(pHVaue);//将块记录添加到块表acBlkTbl.Add(acBlkTblRec);acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);CreateBlockIcon cbi = new CreateBlockIcon();mit();}}#endregion4.2.2 插入带有属性的块参照代码:#region插入带属性的块[CommandMethod("InsertAtrrBlock")]public void InsertAtrrBlock(){Database acCurDb = pDoc.Database;using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) {BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,OpenMode.ForRead) as BlockTable;BlockTableRecord acBlkTblRec =acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],OpenMode.ForWrite) as BlockTableRecord;BlockTableRecord pBlockTableRecord = acTrans.GetObject(acBlkTbl["三角点"], OpenMode.ForRead) as BlockTableRecord;BlockReference bref = new BlockReference(new Point3d(10, 10, 0), pBlockTableRecord.ObjectId);acBlkTblRec.AppendEntity(bref);acTrans.AddNewlyCreatedDBObject(bref, true);foreach (ObjectId id in pBlockTableRecord){Entity ent = (Entity)acTrans.GetObject(id, OpenMode.ForRead, false);//打开当前的对象!if (ent is AttributeDefinition){AttributeReference attRef = new AttributeReference();AttributeDefinition attDef = (AttributeDefinition)ent;attRef.SetPropertiesFrom(attDef);attRef.Height = attDef.Height;attRef.Rotation = attDef.Rotation;attRef.Tag = attDef.Tag;if (attDef.Tag == "三角点名"){attRef.Position = new Point3d(11.3, 10.3, 0);attRef.TextString = "洼地";}else if (attDef.Tag == "高程值"){attRef.Position = new Point3d(11, 8.8, 0);attRef.TextString = "65.99";}if (!bref.IsWriteEnabled){bref.UpgradeOpen();}bref.AttributeCollection.AppendAttribute(attRef);//通知事务处理添加新创建的属性参照acTrans.AddNewlyCreatedDBObject(attRef, true);}}mit();}}#endregion4.3在对话框中查看块定义的图标代码:[CommandMethod("ViewBlock")] public void ViewBlock(){ ViewBlockfrm pViewBlockfrm = new ViewBlockfrm(); pViewBlockfrm.acDoc = pDoc;pViewBlockfrm.Show();}。
CAD技术-实验报告
实验报告课程名称:《CAD技术》实验名称:《CAD技术》课程实验院(系):管理学院专业班级:信息管理与信息系统0801 姓名:吕江博学号:080440110指导教师:聂兴信2011 年04 月15日实验项目二课程名称:《CAD技术》实验类别:本科基础实验实验项目名称:AutoLISP程序设计实验学时:6实验类型:设计性、创新性实验开出要求:必做实验地点:计算中心微机实验室每组人数:1实验日期:2011年03月28日、04月4日、11日一、实验目的和要求目的:进行AutoLISP的程序设计方法练习,掌握AutoCAD二次开发基本技术和方法。
要求:1、利用AutoLISP程序设计语言进行定义以对话框方式绘制圆、矩形等的命令或自己设计 AutoCAD二次开发研究试验。
2、实现参数化绘图等功能。
二、实验内容1. AutoLISP的标准函数使用;2. AutoLISP的程序设计方法练习。
3. 利用AutoLISP程序设计语言进行定义以对话框方式绘制圆、矩形等的命令或自己设计 AutoCAD二次开发研究试验。
三、实验条件硬件条件:计算机一台。
软件条件:AutoCAD2007应用软件、AutoLISP程序设计环境。
四、实验步骤1、启动LISP编辑器,新建程序文件;2、进行绘制圆、矩形等对话框程序文件的编辑,编辑完成后以.DCL为扩展名进行保存;3、通过相应菜单命令选项查看对话框显示效果;4、进行对话框驱动程序文件的编辑,完成后以.LSP为扩展名进行保存;5、加载对话框驱动程序文件,实现参数化绘图等功能。
五、实验结果及分析LISP源程序:1.绘制圆(defun c:yuan()(setvar"cmdecho" 0)(setq cen(getpoint"\n 中心点:"))(setq srr(getvar"circlerad"))(setq str_rr(strcat"\n 圆半径<"(rtos srr 2)">:"))(setq rr(getdist cen str_rr))(if(null rr)(setq rr srr))(command "circle" cen rr)(prin1))2.绘制内接多边形和圆(defun c:neijie()(setvar"cmdecho" 0)(setq cen(getpoint"\n中心点:"))(setq dd(getdist cen "\n直径:"))(command "circle" cen "d" dd)(setq bob(polar cen(/ pi 2)(/ dd 2)))(setq bob1(polar cen(/(* 330 pi) 180)(/ dd 2))) (command "polygon" 6 cen "i" bob)(command "pline" bob1 bob bob2 "")(command "mirror"(entlast) ""cen (polar cen 0 10) "n")(setq int(inters cen (polar cen (/ pi 4) dd) bob bob1)) (command "polygon" 4 cen "i" int)(setq dd2(distance cen int))(setq rr(*(cos(/ pi 4))dd2))(command "circle" cen rr)(prin1))实验结果:1.2.六、讨论及总结问题:第二个程序的图是用手动绘制的,由于第二个程序中的(command "polygon" 6 cen "i" bob)和(command "polygon" 4 cen "i" int)两句程序无法调试成功,其中的最后一个参数不能作用,运行时总是需要多边形半径的输入,不得其解,有待老师指教。
CAD二次实验报告2
CAD开发实验(二)实验二 AutoCAD基本图形对象创建与编辑一、主要内容1、创建直线2、创建圆3、创建圆弧4、创建多段线5、创建椭圆6、样条曲线7、创建面域8、创建文字9、创建填充10、创建尺寸标注11、创建三维实体二、学时安排三、预习内容回顾AutoCAD图形对象创建的一般过程。
四、基本图形对象创建与编辑4.1 基本图形对象创建4.1.1创建直线代码:[CommandMethod("CreatLine")]public void CreatLine(){Database db = HostApplicationServices.WorkingDatabase;using (Transaction trans=db.TransactionManager .StartTransaction() ){//creatlinePoint3d pt1=new Point3d (20,20,0);Point3d pt2=new Point3d (100,100,0);Line lineEnt = new Line(pt1, pt2);BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForRead);BlockTableRecord btr=(BlockTableRecord )trans.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode .ForWrite);PModifyEntID=btr.AppendEntity(lineEnt);trans.AddNewlyCreatedDBObject (lineEnt,true );mit();}}[CommandMethod("ModifyLine")]public void ModifyLine(){Database db = HostApplicationServices.WorkingDatabase;using (Transaction trans = db.TransactionManager.StartTransaction()){Entity pEnt = (Entity)trans.GetObject(PModifyEntID, OpenMode.ForWrite); pEnt.ColorIndex = 1;pEnt.Close();mit();}}4.1.2创建圆代码:[CommandMethod("Cir")]public void Circle(){//声明圆对象Circle c = new Circle();c.Center = new Point3d(0, 0, 0);c.Radius = 100;Database db = HostApplicationServices.WorkingDatabase;//启动事务using (Transaction trans = db.TransactionManager.StartTransaction()){//创建块表BlockTable c_block = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);//读取,0BlockTableRecord c_block_record =(BlockTableRecord)trans.GetObject(c_block[BlockTableRecord.ModelSpace],OpenMode.ForWrite);//写入,1c_block_record.AppendEntity(c);//块表记录追加实体,把图形对象的记录加入到块表记录trans.AddNewlyCreatedDBObject(c, true);//将圆添加到事务处理中mit();//提交事务}}4.1.3创建圆弧代码://创建圆弧[CommandMethod("CreatAre")]public void CreatArc(){ Database db = HostApplicationServices.WorkingDatabase;using (Transaction trans = db.TransactionManager.StartTransaction()){ Arc pArc = new Arc(new Point3d(50, 50, 0), 100, 0, 100);BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);BlockTableRecord btr =(BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); btr.AppendEntity(pArc);trans.AddNewlyCreatedDBObject(pArc, true);mit();}}4.1.4创建多段线代码://创建多段线[CommandMethod("CPloyline")]public void createPloyline1(){ Polyline pl = new Polyline(5);Database db = HostApplicationServices.WorkingDatabase;using (Transaction trans = db.TransactionManager.StartTransaction()){ Point2d p1 = new Point2d(50, 10);Point2d p2 = new Point2d(100, 30);Point2d p3 = new Point2d(150, 60);Point2d p4 = new Point2d(200, 90);Point2d p5 = new Point2d(250, 120);Point2dCollection pc = new Point2dCollection();pc.Add(p1);pc.Add(p2);pc.Add(p3);pc.Add(p4);pc.Add(p5);for (int i = 0; i < 5; i++){ pl.AddVertexAt(i, pc[i], 0.5, 5, 5);}BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForRead);BlockTableRecord btr =(BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);btr.AppendEntity(pl);trans.AddNewlyCreatedDBObject(pl, true);mit();}}4.1.5创建椭圆代码://创建椭圆[CommandMethod("CEllipse")]public void Ellipse(){Database db = HostApplicationServices.WorkingDatabase;using (Transaction trans = db.TransactionManager.StartTransaction()){Ellipse pEllipse = new Ellipse(new Point3d(150, 150, 0), new Vector3d(0, 0, 50), new Vector3d(50, 50, 0), 0.8, 0, 0);BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);BlockTableRecord btr =(BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); btr.AppendEntity(pEllipse);trans.AddNewlyCreatedDBObject(pEllipse, true);mit();}}4.1.6样条曲线代码://创建样条曲线[CommandMethod("CSpline")]public void CreateSpline(){ Database db = HostApplicationServices.WorkingDatabase;using (Transaction trans = db.TransactionManager.StartTransaction()){ Point3d p1 = new Point3d(50, 30, 0);Point3d p2 = new Point3d(100, 60, 0);Point3d p3 = new Point3d(150, 520, 0);Point3d p4 = new Point3d(200, 240, 0);Point3d p5 = new Point3d(250, 300, 0);Point3dCollection pc = new Point3dCollection();pc.Add(p1);pc.Add(p2);pc.Add(p3);pc.Add(p4);pc.Add(p5);Spline spline = new Spline(pc, 2, 5);BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);BlockTableRecord btr =(BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); btr.AppendEntity(spline);trans.AddNewlyCreatedDBObject(spline, true);mit();}}4.1.7创建面域代码://创建面域[CommandMethod("CPolygon")]public void CreatePolygon(){ Database db = HostApplicationServices.WorkingDatabase;using (Transaction Trans = db.TransactionManager.StartTransaction()){ Point3d center = new Point3d(50, 50, 0);Circle entity = new Circle(center, Vector3d.ZAxis, 150);DBObjectCollection curveSegments = new DBObjectCollection();DBObjectCollection regions = new DBObjectCollection();curveSegments.Add(entity);regions = Region.CreateFromCurves(curveSegments);Region newRegion = (Region)regions[0];newRegion.SetDatabaseDefaults();BlockTable bt = (BlockTable)Trans.GetObject(db.BlockTableId, OpenMode.ForRead);BlockTableRecord btr =(BlockTableRecord)Trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); btr.AppendEntity(newRegion);Trans.AddNewlyCreatedDBObject(newRegion, true);mit();}}4.1.8创建文字代码://创建文字[CommandMethod("AddText")]public void AddText(){Database db = HostApplicationServices.WorkingDatabase;using (Transaction trans = db.TransactionManager.StartTransaction()){DBText text = new DBText();text.Position = new Point3d(5, 5, 0);text.ColorIndex = 3;text.TextString = "欢迎来到CAD!";MText text1 = new MText();text1.Color=Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 2);BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForRead);BlockTableRecord btr =(BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);btr.AppendEntity(text);btr.AppendEntity(text1);trans.AddNewlyCreatedDBObject(text, true);trans.AddNewlyCreatedDBObject(text1, true);mit();}}4.1.9创建填充代码://创建填充[CommandMethod("AddHatch")]public void AddHatch(){Database db = HostApplicationServices.WorkingDatabase;using (Transaction trans = db.TransactionManager.StartTransaction()){Circle circle = new Circle();circle.Center = new Point3d(150, 150, 0);circle.Radius = 200; BlockTable bt =(BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);BlockTableRecord btr =(BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); btr.AppendEntity(circle);trans.AddNewlyCreatedDBObject(circle, true);ObjectIdCollection collection = new ObjectIdCollection();collection.Add(circle.ObjectId);Hatch hatch = new Hatch();hatch.Elevation = 0;hatch.HatchStyle = HatchStyle.Normal;hatch.ColorIndex = 5;hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");hatch.AppendLoop(HatchLoopTypes.Default, collection);hatch.EvaluateHatch(true);btr.AppendEntity(hatch);trans.AddNewlyCreatedDBObject(hatch, true);mit();}}4.1.10创建标注代码:// 创建半径标注[CommandMethod("CRadialDimension")]public static void CreateRadialDimension(){ Document acDoc =Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;Database acCurDb = acDoc.Database;using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { BlockTable acBlkTbl;acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,OpenMode.ForRead) as BlockTable;BlockTableRecord acBlkTblRec;acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;RadialDimension acRadDim = new RadialDimension();acRadDim.SetDatabaseDefaults();acRadDim.Center = new Point3d(0, 0, 0);acRadDim.ChordPoint = new Point3d(25, 25, 0);acRadDim.LeaderLength = 5;acRadDim.DimensionStyle = acCurDb.Dimstyle;acBlkTblRec.AppendEntity(acRadDim);acTrans.AddNewlyCreatedDBObject(acRadDim, true);mit();}}4.1.11创建三维实体代码://创建三维实体[CommandMethod("Create3DSolid")]public void Create3DSolid(){ Database db = HostApplicationServices.WorkingDatabase;using (Transaction trans = db.TransactionManager.StartTransaction()){ Point3d p1 = new Point3d(50, 50, 0);Point3d p2 = new Point3d(110, 100, 0);Point3d p3 = new Point3d(150, 160, 0);Point3d p4 = new Point3d(210, 190, 0);Point3d p5 = new Point3d(270, 230, 0);Point3dCollection pc = new Point3dCollection();pc.Add(p1);pc.Add(p2);pc.Add(p3);pc.Add(p4);pc.Add(p5);Polyline3d Polyline3d = new Polyline3d(0, pc, true);BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);BlockTableRecord btr =(BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);DBObjectCollection dbobc = new DBObjectCollection();DBObjectCollection regions = new DBObjectCollection();dbobc.Add(Polyline3d);regions = Region.CreateFromCurves(dbobc);Region region = (Region)regions[0];Solid3d sd = new Solid3d();sd.Revolve(region, new Point3d(0, 0, 0), Vector3d.XAxis, 360); PModifyEntID = btr.AppendEntity(sd);trans.AddNewlyCreatedDBObject(sd, true);mit();}}。
Autocad 的二次开发第一部分:实验分析与设计一、实验内容描述1 ...
Autocad的二次开发第一部分:实验分析与设计一、实验内容描述1、通过示例程序了解Autocad的二次开发的基本概念,以及基本实现方法。
2、通过vb程序示例初步了解Autocad对象模型,以及面向对象开发的基本概念。
3、通过lisp程序示例的调试运行,初步掌握lisp语言的格式以及Autocad的lisp 编程方法。
二、实验基本原理与设计基本原理:Autocad作为CAD支撑软件,提供了一个通用的设计和绘图平台,已经被广泛使用。
该软件提供了较好的二次开发支持。
其中主要方法,一种是Autocad自身内嵌的lisp开发工具;另外一种主要的开发方法是通过Autocad提供的对象模型,使用其它的软件进行二次开发,如支持VB、VC等高级编程语言。
在Autocad中内嵌的Lisp开发程序随着Autocad的不断升级换代已经发展到Visual Lisp,提供了可视化的编程环境,可及时调试,使用方便,并可直接调用Autocad命令,方便易用,是进行简单开发的最便利的工具。
面向对象的开发使得二次开发不仅局限于Autocad本身的功能,使用户能更好地实现专用化的开发。
本次实验进行基于lisp的开发演示和基于VB的开发演示。
注:1)vb开发autocad需要连接autocad(不同的autocad版本调用方式有所区别,本实例针对autocad2000版),vba开发则可直接调用thisdrawing对象。
2)步骤:连接autocad,定义autocad类型变量并实例化,通过对autocad对象的属性和方法的调用,实现绘图等操作。
3)可适当设计界面实现参数化特征化绘图。
4)本实验可以自行查找实例并运行体验。
Vb(vba)开发autocad:根据autocad对象模型,可建立如下vb程序,第一个函数Sub ConnectToAcad()可实现autocad的启动,第二个函数可实现启动并在autocad中画线。
以vb6.0为例:、启动vb,新建工程,1)在form1上添加按钮,如图红箭头处,按钮标题可以修改显示文字,如图2;2)分别双击“启动”和“画线”按钮,分别加入代码ConnectToAcad,如图3;和AddLineVB,如图3;3)加入对autocad的引用,如图4;(autocad版本不同引用方式可能有所不同)4)运行程序。
CAD二次开发方法研究与实现
CAD二次开发方法研究与实现随着计算机辅助设计(CAD)技术的不断发展,越来越多的行业开始采用CAD软件进行产品设计和开发。
然而,市面上的CAD软件往往不能满足特定领域的需求,因此,CAD二次开发应运而生。
本文将介绍CAD二次开发方法的基本原理和常用工具,并通过实际案例探究其应用效果和优缺点,最后总结CAD二次开发方法的应用前景和未来发展方向。
本文的主题为CAD二次开发方法研究与实现。
通过对CAD二次开发的基本原理、常用工具、案例探究和技术总结等方面的阐述,帮助读者了解CAD二次开发的重要性和实现方法。
CAD二次开发是指基于现有的CAD软件平台,根据特定领域的需求,定制化开发适合用户需求的软件工具或系统。
它通常包括界面设计、功能开发、数据处理和图形输出等环节。
(1)AutoLISP:AutoLISP是AutoCAD内置的编程语言,易于学习,适合初学者。
它可以通过编写程序对AutoCAD进行定制化操作,实现二维图形绘制、三维模型构建等功能。
(2)Visual Basic for Applications(VBA):VBA是微软开发的一种编程语言,它可以在AutoCAD中通过宏记录器进行二次开发。
VBA 语法相对较为严谨,具有较好的调试性能,适用于复杂的定制化开发项目。
(3)C++和ObjectARX:对于大型的二次开发项目,可能需要使用C++和ObjectARX等更为高级的语言进行开发。
ObjectARX是Autodesk公司开发的一套基于C++的CAD二次开发库,提供了丰富的API接口,可以实现高性能、复杂的功能开发。
(1)模块化编程:将复杂的功能划分为多个独立的模块,每个模块都具有特定的功能。
通过模块的组合和调用,实现整个系统的功能。
这种模块化编程方式可以提高代码的可重用性和可维护性。
(2)参数化设计:在CAD二次开发中,参数化设计是一种常见的开发方式。
通过定义一系列的参数来描述几何图形、约束关系等,从而实现图形的参数化。
CAD二次开发实验1
实验一 ObjectARX .NET托管开发包的使用一、主要内容1、下载安装ObjectARX .NET托管开发包2、创建AutoCAD命令3、创建自定义用户菜单4、创建自定义用户工具条5、创建用户自定义窗体二、学时安排三、预习内容及要求回顾ObjectARX API和其托管API之间的对应关系;掌握不同API版本对应的开发版本;熟悉创建用户需求所要引用命名空间。
开发语言使用C#,AutoCAD版本2010,objectARX2010。
四、下载安装ObjectARX4.1下载ObjectARX不同的ACAD版本用不同的开发包及开发环境ACAD2000~ACAD2002 : VC++ 6.0ACAD2004~ACAD2006 : 2002ACAD2007~ACAD2009 : 2005ACAD2010~ACAD2011 : 2008ACAD2012~ : 2010下载ObjectARX在打开打开默认网页浏览器并浏览网站.的网页上,点击License & Download(许可&下载).填写必填字段并选择ObjectARX for AutoCAD 2010. 点击Submit(提交)按扭.在下载页, 点击Download Now(现在下载)使用下载管理器或点击Standard Download Method(标准下载方法)使用你的网页浏览器的默认下载方法下载.点击Save(保存)或用于保存文件到你的本地驱动器的选项。
指定下载ObjectARX SDK安装包文件存放的位置。
一旦安装包文件下载完成,就浏览你保存在本地的安装并双击它,安装向导将显示在ObjectARX <释放>对话框中, 指定一个新的安装位置或接受默认的安装位置。
点击Install(安装)按扭. 如果没有遇到问题,安装向导完成后关闭它安装ObjectARX向导和托管项目模板一旦ObjectARX SDK被安装,请使用默认目录c:\ObjectARX <Release>浏览它的安装文件夹。
《AutoCAD二次开发技术》实验指导书
《AutoCAD二次开发》实验指导书环境与建筑工程学院测绘教研室实验1 Visual Lisp开发环境的认识和程序运行方法【实验目的】1.熟悉所用计算机系统的基本操作方法。
2.学习Visual Lisp的使用方法,掌握程序编辑、编译、打包、运行及查看运行结果的方法。
3.掌握Visual Lisp程序的基本结构。
【实验内容】1.熟悉使用的计算机系统的基本操作,创建自己的工作目录,参照附录中介绍的方法,掌握Visual Lisp的启动方法(一种或多种),了解AutoCAD系统的安装路径和结构。
2.进入Visual Lisp的工作环境,熟悉Visual LISP 用户界面。
3.熟悉Visual Lisp的系统菜单组成及功能。
学习使用功能键和快捷键调用菜单项的方法。
掌握文件建立、编辑、修改和保存的方法。
落实文件的存储位置是否是你的工作目录,如果不是,回第二步重新设置。
4.了解控制台窗口5.了解Visual LISP 文本编辑器6.加载和运行AutoLISP 程序7.退出Visual LISP实验2 Visual Lisp应用程序【实验目的】1.掌握Visual Lisp语言基本数据类型的概念,理解数据类型与运算的关系。
2.熟悉简单变量的定义和赋值方法,掌握不同类型数据之间赋值的规律。
3.学习Visual Lisp语言运算符优先级和结合性的概念,掌握算术表达式的计算方法及表达式计算中的类型转换方法。
4.掌握数学表达式的程序表示方法。
5.进一步熟悉Visual Lisp程序的编辑、编译、连接和运行的过程。
【实验内容】1.掌握用Visual LISP 开发程序步骤2.使用控制台窗口3.使用文本编辑器4.使用控制台和编辑器的编码辅助工具5.用Visual LISP 设置代码格式6.检查语法错误7.通过样例学习调试程序8.使用Visual LISP 调试功能9.使用Visual LISP 数据查看工具10.编译和链接程序11.创建应用程序模块样例程序:(defun yinyang (/ origin radius i-radius half-r origin-x origin-y os) (setq os (getvar "OSMODE")) ;; Save OSNAP mode(setvar "OSMODE" 0) ;; Turn off OSNAP(setq origin (getpoint "\nOrigin of inyn sign: "))(setq radius (getdist "\nRadius of inyn sign: " origin))(setq i-radius (getdist "\nRadius of internal circle: "origin))(if (> i-radius radius) (setq i-radius (/ radius 4)))(setq half-r (/ radius 2))(setq origin-x (car origin))(setq origin-y (cadr origin))(command "_.CIRCLE" origin radius)(command "_.ARC""_C"(list origin-x (+ origin-y half-r)) ;;center(list origin-x (+ origin-y radius)) ;;start pointorigin ;;end point)(command "_.ARC""_C"(list origin-x (- origin-y half-r)) ;;center(list origin-x (- origin-y radius)) ;;start pointorigin ;;end point)(command "_.CIRCLE"(list origin-x (+ origin-y half-r));;centrei-radius)(command "_.CIRCLE"(list origin-x (- origin-y half-r));;centrei-radius)(setvar "OSMODE" os) ;; Restore OSNAP mode(princ))实验3 Visual Lisp程序设计【实验目的】1.熟练掌握AutoLISP 表达式。
cad二次开发报告
全局变量Option ExplicitDim adoConn As ADODB.Connection '连接对象Dim adoRs As Recordset '记录集对象Dim strPath As StringPublic p As Single '传递功率Public Pd As Single '设计功率Public C As Single '传动比Public cr As Single '实际传动比Public n1 As Single '小带轮转速Public n2 As Single '大带轮转速Public Ka As Single '工况系数Public X As Integer '带的型号0-Y,1-Z,2-A,3-B,4-C,5-D,6-EPublic dd1 As Long '小带轮基准直径Public dd2 As Long '大带轮基准直径Public beltz As Integer '带数Public belt(6) As String 'V带尺寸Public ddi As LongPublic D As DoubleDim pt0(2) As Double '起点Dim pt1(2) As Double, pt2(2) As Double 'V槽曲线的端点Public Ld As Single '实际带长Public a As Integer '实际中心距Public angle As Single '包角Public KL As Single, h As Single, ha As SinglePublic VbeltData(6) As VariantPublic Vsize(6) As VariantPublic Vbd As Double, Vha As Double, Vhf As Double, Ve As Double, Ve0 As Double, Vf As Double, Vf0 As Double, Vdelta As DoublePublic VB As Double, Vda1 As Double, Vphi1 As Double, Vda2 As Double, Vphi2 As Double, Vdai As Double, Vphii As DoublePublic Sub BeltDesign()belt(0) = "Y": belt(1) = "Z": belt(2) = "A": belt(3) = "B": belt(4) = "C": belt(5) = "D": belt(6) = "E"'V带基本数据赋值VbeltData(0) = Array(5.3, 6, 4, 18, 1.6, 0.02)VbeltData(1) = Array(8.5, 10, 6, 47, 2#, 0.06)VbeltData(2) = Array(11#, 13, 8, 81, 2.75, 0.1)VbeltData(3) = Array(14#, 17, 11, 138, 3.5, 0.17)VbeltData(4) = Array(19#, 22, 14, 230, 4.8, 0.3)VbeltData(5) = Array(27#, 32, 19, 476, 8.1, 0.62)VbeltData(6) = Array(32#, 38, 23, 692, 9.6, 0.9)'V带轮槽尺寸Vsize(0) = Array(5.3, 1.6, 4.7, 8, 0.3, 7, 1, 5) 'bd,ha,hf,e,e+,f,f+,deltaVsize(1) = Array(8.5, 2#, 7#, 12, 0.3, 8, 1, 5.5)Vsize(2) = Array(11#, 2.75, 8.7, 15, 0.3, 10, 2, 6)Vsize(3) = Array(14#, 3.5, 10.8, 19, 0.4, 12.5, 2, 7.5)Vsize(4) = Array(19#, 4.8, 14.3, 25.5, 0.5, 17, 3, 10)Vsize(5) = Array(27#, 8.1, 19.9, 37, 0.6, 24, 4, 12)Vsize(6) = Array(32#, 9.6, 23.4, 44.5, 0.7, 29, 5, 15)'带传动设计.ShowEnd SubPublic Function ConnDB() As BooleanOn Error GoTo ERRORHANDLERstrPath = ThisDrawing.Application.VBE.ActiveVBProject.FileNameIf adoConn Is Nothing ThenstrPath = ThisDrawing.Application.VBE.ActiveVBProject.FileNameSet adoConn = New ADODB.ConnectionadoConn.CursorLocation = adUseClientadoConn.Provider = "Microsoft.ACE.OLEDB.12.0;"strPath = Left(strPath, Len(strPath) - 9) & "传送带数据库.accdb"adoConn.Open strPathEnd IfConnDB = TrueExit FunctionERRORHANDLER:Set adoConn = NothingMsgBox "Error occured in procedure 'EnsureBackendIsConnected'" & vbCrLf & vbCrLf & _ "Error Number: " & Err.Number & vbCrLf & _"Description: " & Err.DescriptionEnd FunctionPublic Sub readRecordset(record As ADODB.Recordset, strSQl As String, _Optional LockType As LockTypeEnum = adLockOptimistic, _Optional CursorType As CursorTypeEnum = adOpenKeyset)On Error GoTo ERRORHANDLERDim FormRecordset As ADODB.RecordsetIf ConnDB() = True ThenSet FormRecordset = New ADODB.RecordsetWith FormRecordsetSet .ActiveConnection = adoConn.Source = strSQl.LockType = LockType.CursorType = CursorType.Open ' If this fails, error handler will kick in End WithSet record = FormRecordsetElseMsgBox "readRecordset error: ADO backend connection failed"End IfExit SubERRORHANDLER:Set FormRecordset = Nothing ' not strictly neededMsgBox "Error occured in procedure 'readRecordset'" & vbCrLf & vbCrLf & _"Error Number: " & Err.Number & vbCrLf & _"Description: " & Err.DescriptionEnd Sub'图层创建函数Public Function NewLayer(ByVal name As String, ByVal layer As AcadLayer) As Boolean If HasLayer(name) ThenNewLayer = FalseSet layer = NothingElseSet layer = yers.Add(name)NewLayer = TrueEnd IfEnd Function'图层查找函数Public Function HasLayer(ByVal name As String)Dim layer As AcadLayerFor Each layer In yersIf StrComp(, name, vbTextCompare) = 0 ThenHasLayer = TrueExit FunctionEnd IfNext layerHasLayer = FalseEnd Function'图层删除函数Public Function DeleteLayer(ByVal name As String)Debug.Assert (HasLayer(name))Dim layer As AcadLayerSet layer = youts.Item(name)On Error Resume Nextlayer.DeleteIf Err ThenDeleteLayer = FalseElseDeleteLayer = TrueEnd IfEnd Function'图层打开、关闭函数Public Sub TurnOnlayer(ByVal name As String, Optional bOpen As Boolean = True) Debug.Assert (HasLayer(name))Dim layer As AcadLayerSet layer = yers.Item(name)If bOpen ThenyerOn = TrueElseyerOn = FalseEnd IfEnd Sub'设置当前图层Public Sub SetCurrentLayer(LayerName)Dim entry As AcadLayerFor Each entry In yersIf = LayerName ThenThisDrawing.ActiveLayer = entryExit SubEnd IfNextEnd SubPublic Function GetVWheelData()'bd,ha,hf,e,e+,f,f+,deltaVbd = Vsize(X)(0)Vha = Vsize(X)(1)Vhf = Vsize(X)(2)Ve = Vsize(X)(3)Ve0 = Vsize(X)(4)Vf = Vsize(X)(5)Vf0 = Vsize(X)(6)Vdelta = Vsize(X)(7)VB = (beltz - 1) * Ve + 2 * VfVda1 = dd1 + 2 * VhaVda2 = dd2 + 2 * VhaSelect Case (X)Case 0 'YIf dd1 <= 60 ThenVphi1 = 32ElseVphi1 = 36End IfIf dd2 <= 60 ThenVphi2 = 32ElseVphi2 = 36End IfCase 1 'ZIf dd1 <= 80 ThenVphi1 = 34ElseVphi1 = 38End IfIf dd2 <= 80 ThenVphi2 = 34ElseVphi2 = 38End IfCase 2 'AIf dd1 <= 118 ThenVphi1 = 34ElseVphi1 = 38End IfIf dd2 <= 118 ThenVphi2 = 34ElseVphi2 = 38End IfCase 3 'BIf dd1 <= 190 ThenVphi1 = 34ElseVphi1 = 38End IfIf dd2 <= 190 ThenVphi2 = 34ElseVphi2 = 38End IfCase 4 'CIf dd1 <= 315 ThenVphi1 = 34ElseVphi1 = 38End IfIf dd2 <= 315 ThenVphi2 = 34ElseVphi2 = 38End IfCase 5 'DIf dd1 <= 475 ThenVphi1 = 36ElseVphi1 = 38End IfIf dd2 <= 475 ThenVphi2 = 36ElseVphi2 = 38End IfCase 6 'EIf dd1 <= 600 ThenVphi1 = 36ElseVphi1 = 38End IfIf dd2 <= 600 ThenVphi2 = 36ElseVphi2 = 38End IfEnd SelectEnd FunctionPublic Function drawV(ptx As Double, pty As Double, beltz As Integer, Vphi As Double) As AcadEntityDim LineObject As AcadLineDim ptNum As IntegerDim ptx0 As DoubleptNum = 4 * beltz * 2 - 1 'V槽曲线所含的点数Dim p() As Double '定义点坐标ReDim p(ptNum)Dim i As IntegerFor i = 0 To ptNum Step 8ptx0 = ptx + Ve * i / 8p(i) = ptx0 - Vbd / 2 - Tan(Vphi * 3.14 / 360) * Vhap(i + 1) = ptyp(i + 2) = ptx0 - Vbd / 2 + Tan(Vphi * 3.14 / 360) * Vhfp(i + 3) = pty - Vha - Vhfp(i + 4) = ptx0 + Vbd / 2 - Tan(Vphi * 3.14 / 360) * Vhfp(i + 5) = pty - Vha - Vhfp(i + 6) = ptx0 + Vbd / 2 + Tan(Vphi * 3.14 / 360) * Vhap(i + 7) = ptyNext iSet drawV = ThisDrawing.ModelSpace.AddLightWeightPolyline(p) '画多段线pt1(0) = p(0): pt1(1) = p(1)pt2(0) = p(ptNum - 1): pt2(1) = p(ptNum)End Function'设置图层Public Sub User_CreateLayers()Call User_CreateLayer("粗实线层")Call User_CreateLayer("中心线层")Call User_CreateLayer("虚线层")Call User_CreateLayer("细实线层")Call User_CreateLayer("标注层")Call User_CreateLayer("剖面线层")End SubPublic Sub User_LoadLinetype(LinetypeName)On Error Resume NextThisDrawing.Linetypes.Load LinetypeName, "acad.lin"Exit SubEnd Sub'编辑图层Public Sub User_CreateLayer(LayerName As String)Dim LayerObject As AcadLayerSet LayerObject = yers.Add(LayerName)If LayerName = "粗实线层" ThenLayerObject.Lineweight = acLnWt030ElseIf LayerName = "中心线层" ThenCall User_LoadLinetype("Center2")LayerObject.Color = acRedLayerObject.Linetype = "Center2"ElseIf LayerName = "虚线层" ThenCall User_LoadLinetype("dashed2")LayerObject.Color = acWhiteLayerObject.Linetype = "dashed2"ElseIf LayerName = "细实线层" ThenLayerObject.Lineweight = acLnWt015LayerObject.Color = acBlueElseIf LayerName = "标注层" ThenLayerObject.Color = acYellowElseIf LayerName = "剖面线层" ThenLayerObject.Lineweight = acLnWt015LayerObject.Color = acGreenEnd IfThisDrawing.SetVariable "Ltscale", 20 '设置线型比例的系统变量End SubPublic Function cucaodu(ptx As Double, pty As Double, ccd As Single, angle As Double, font As Single)Dim Line(0 To 2) As AcadLineDim TextObject As AcadTextDim Text As StringDim textHeight As SingleDim Pi As SinglePi = 3.1415926Text = ccd'确定文字高度textHeight = fontDim PointOne(0 To 2) As DoubleDim PointTwo(0 To 2) As DoubleDim PointThr(0 To 2) As DoubleDim PointFou(0 To 2) As DoubleDim txtPoint(0 To 2) As DoubleDim Alf As Double '槽与水平线夹角Dim Dist As Double '三角形边长Dist = 1.4 * textHeight / Sin(Pi / 3)Alf = angle * Pi / 180PointOne(0) = ptx: PointOne(1) = pty: PointOne(2) = 0PointTwo(0) = PointOne(0) + 2 * Dist * Cos(Alf + Pi / 3)PointTwo(1) = PointOne(1) + 2 * Dist * Sin(Alf + Pi / 3): PointTwo(2) = 0PointThr(0) = PointOne(0) + Dist * Cos(Alf + 2 * Pi / 3)PointThr(1) = PointOne(1) + Dist * Sin(Alf + 2 * Pi / 3): PointThr(2) = 0PointFou(0) = PointOne(0) + Dist * Cos(Alf + Pi / 3)PointFou(1) = PointOne(1) + Dist * Sin(Alf + Pi / 3): PointFou(2) = 0Set Line(0) = ThisDrawing.ModelSpace.AddLine(PointOne, PointTwo)Set Line(1) = ThisDrawing.ModelSpace.AddLine(PointOne, PointThr)Set Line(2) = ThisDrawing.ModelSpace.AddLine(PointThr, PointFou)Dim TextInsertPoint(0 To 2) As DoubleTextInsertPoint(0) = PointFou(0) + Cos(Alf + Pi / 2)TextInsertPoint(1) = PointFou(1) + Sin(Alf + Pi / 2): TextInsertPoint(2) = 0Set TextObject = ThisDrawing.ModelSpace.AddText(Text, TextInsertPoint, textHeight) If Alf <= Pi / 2 Or Alf > 3 * Pi / 2 ThenTextObject.Rotate TextInsertPoint, AlfTextObject.Alignment = acAlignmentRightElseIf Pi / 2 < Alf And Alf <= 3 * Pi / 2 ThenTextObject.Rotate TextInsertPoint, Alf - PiTextObject.Alignment = acAlignmentTopLeftEnd IfTextObject.TextAlignmentPoint = TextInsertPointTextObject.UpdateThisDrawing.Regen acAllViewportsEnd Function'绘制图形'绘制第一种结构带轮Public Sub DrawWheel1()Dim p1(2) As Double, p2(2) As Double, p3(2) As Double, p4(2) As DoubleDim L As DoubleDim lin As AcadEntityDim HatchObject As AcadHatchDim outerloop(9) As AcadEntityIf VB > 2 * D ThenMsgBox "孔径小于允许值", vbOKOnly, "警告"Exit SubEnd IfUser_CreateLayersThisDrawing.ActiveLayer = yers.Item("粗实线层") pt0(0) = 0: pt0(1) = ddi / 2 + Vha: pt0(2) = 0'绘制第一个外边界Set lin = drawV(pt0(0) + Vf, pt0(1), beltz, Vphii)Set outerloop(0) = linp1(0) = pt1(0): p1(1) = pt1(1): p1(2) = pt1(2)Set lin = ThisDrawing.ModelSpace.AddLine(p1, pt0)Set outerloop(1) = linp2(0) = pt2(0): p2(1) = pt2(1): p2(2) = pt2(2)p1(0) = pt0(0) + VB: p1(1) = pt0(1): p1(2) = pt0(2)Set lin = ThisDrawing.ModelSpace.AddLine(p1, p2)Set outerloop(2) = linp2(0) = p1(0): p2(1) = 0.9 * DSet lin = ThisDrawing.ModelSpace.AddLine(p1, p2)Set outerloop(3) = linIf VB >= 1.5 * D ThenL = 2 * DElseL = VBEnd Ifp1(0) = p2(0) + L - VB: p1(1) = 0.9 * DSet lin = ThisDrawing.ModelSpace.AddLine(p1, p2)Set outerloop(4) = linp2(0) = p1(0): p2(1) = D / 2 + 2Set lin = ThisDrawing.ModelSpace.AddLine(p1, p2)Set outerloop(5) = linp1(0) = p2(0) - 2: p1(1) = D / 2Set lin = ThisDrawing.ModelSpace.AddLine(p1, p2)Set outerloop(6) = linp2(0) = 2: p2(1) = p1(1)Set lin = ThisDrawing.ModelSpace.AddLine(p1, p2)Set outerloop(7) = linp1(0) = 0: p1(1) = p2(1) + 2Set lin = ThisDrawing.ModelSpace.AddLine(p1, p2)Set outerloop(8) = linSet lin = ThisDrawing.ModelSpace.AddLine(p1, pt0)Set outerloop(9) = lin'绘制剖面线ThisDrawing.ActiveLayer = yers.Item("剖面线层")Set HatchObject = ThisDrawing.ModelSpace.AddHatch(0, "ansi31", True) HatchObject.AppendOuterLoop (outerloop)HatchObject.PatternScale = 1.5HatchObject.Evaluatep1(0) = 0: p1(1) = 0p2(0) = 100: p2(1) = 0' '图形镜像Dim z As IntegerFor z = 0 To 9outerloop(z).Mirror p1, p2NextDim hatObj As AcadHatchSet hatObj = HatchObject.Mirror(p1, p2)' 改变镜像后剖面线的角度hatObj.PatternAngle = 0'绘制其余边界ThisDrawing.Regen TrueThisDrawing.ActiveLayer = yers.Item("粗实线层")p1(0) = pt0(0): p1(1) = D / 2 + 2p2(0) = pt0(0): p2(1) = -1 * D / 2 - 2Set lin = ThisDrawing.ModelSpace.AddLine(p1, p2)p1(0) = pt0(0) + 2: p1(1) = D / 2p2(0) = pt0(0) + 2: p2(1) = -1 * D / 2Set lin = ThisDrawing.ModelSpace.AddLine(p1, p2)p1(0) = pt0(0) + L: p1(1) = D / 2 + 2p2(0) = pt0(0) + L: p2(1) = -1 * D / 2 - 2Set lin = ThisDrawing.ModelSpace.AddLine(p1, p2)p1(0) = pt0(0) + L - 2: p1(1) = D / 2p2(0) = pt0(0) + L - 2: p2(1) = -1 * D / 2Set lin = ThisDrawing.ModelSpace.AddLine(p1, p2)'绘制中心线ThisDrawing.ActiveLayer = yers.Item("中心线层") p1(0) = -10: p1(1) = 0p2(0) = 10 + L: p2(1) = 0Set lin = ThisDrawing.ModelSpace.AddLine(p1, p2)p1(1) = ddi / 2: p2(1) = ddi / 2 '带轮基准直径线Set lin = ThisDrawing.ModelSpace.AddLine(p1, p2)p1(1) = -1 * ddi / 2: p2(1) = -1 * ddi / 2Set lin = ThisDrawing.ModelSpace.AddLine(p1, p2)'槽中心线Dim q As IntegerFor q = 0 To beltz - 1p1(0) = Vf + Ve * q: p1(1) = ddi / 2 + Vha + 5p2(0) = Vf + Ve * q: p2(1) = ddi / 2 - Vhf - 5Set lin = ThisDrawing.ModelSpace.AddLine(p1, p2)p3(0) = Vf + Ve * q: p3(1) = -(ddi / 2 + Vha + 5)p4(0) = Vf + Ve * q: p4(1) = -(ddi / 2 - Vhf - 5)Set lin = ThisDrawing.ModelSpace.AddLine(p3, p4)Next'尺寸标注ThisDrawing.ActiveLayer = yers.Item("标注层")Dim dimLn As AcadDimAlignedp1(0) = L: p1(1) = D / 2p2(0) = L: p2(1) = -1 * D / 2p3(0) = L + 20: p3(1) = 0: p3(2) = 0Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3) dimLn.TextOverride = "%%c<>"If L > VB Thenp1(0) = L: p1(1) = 0.9 * Dp2(0) = L: p2(1) = -0.9 * Dp3(0) = L + 30: p3(1) = 0: p3(2) = 0Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3) dimLn.TextOverride = "%%c<>"End Ifp1(0) = 0: p1(1) = ddi / 2p2(0) = 0: p2(1) = -1 * ddi / 2p3(0) = -30: p3(1) = 0: p3(2) = 0Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3) dimLn.TextOverride = "%%c<>"p1(0) = 0: p1(1) = ddi / 2 + Vhap2(0) = 0: p2(1) = -1 * ddi / 2 - Vhap3(0) = -40: p3(1) = 0: p3(2) = 0Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3)dimLn.TextOverride = "%%c<>"p1(0) = 0: p1(1) = 0p2(0) = L: p2(1) = 0p3(0) = L / 2: p3(1) = -1 * ddi / 2 - Vha - 30: p3(2) = 0Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3)dimLn.TextOverride = "<>"'标注槽尺寸p1(0) = Vf - Vbd / 2: p1(1) = ddi / 2p2(0) = Vf + Vbd / 2: p2(1) = p1(1)p3(0) = Vf: p3(1) = p1(1) + Vha + 10Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3)p1(0) = 0: p1(1) = ddi / 2 + Vhap2(0) = Vf: p2(1) = p1(1)p3(0) = Vf / 2: p3(1) = p1(1) + 20Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3)Dim ts As Stringts = "\A1;\H2;<>\H0.75x;\S+2^-1"dimLn.TextOverride = tsdimLn.UpdateIf beltz > 1 Thenp1(0) = Vf + Ve: p1(1) = ddi / 2 + Vhap2(0) = Vf: p2(1) = p1(1)p3(0) = Vf / 2: p3(1) = p1(1) + 20Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3)ts = "\A1;\H2;<>\H0.75x;%%p0.4"dimLn.TextOverride = tsdimLn.UpdateEnd Ifp1(0) = 0: p1(1) = ddi / 2 + Vhap2(0) = VB: p2(1) = p1(1)p3(0) = VB / 2: p3(1) = p1(1) + 30Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3)p1(0) = Vf - Vbd / 2 + Tan(Vphii * 3.14 / 360) * Vhf: p1(1) = ddi / 2 - Vhf p2(0) = Vf + Vbd / 2 - Tan(Vphii * 3.14 / 360) * Vhf: p2(1) = p1(1)p3(0) = Vf: p3(1) = p1(1) - 40pt0(0) = Vf: pt0(1) = ddi / 2 - Vbd / (2 * Tan(Vphii * 3.14 / 360))Call ThisDrawing.ModelSpace.AddDimAngular(pt0, p1, p2, p3)'粗糙度ThisDrawing.ActiveLayer = yers.Item("粗实线层")p1(0) = Vf - Vbd / 2 + Tan(Vphii * 3.14 / 360): p1(1) = ddi / 2 - 1Call cucaodu(p1(0), p1(1), 3.2, -(90 - Vphii / 2), 3)ZoomExtentsEnd Sub'绘制第二种结构带轮Public Sub DrawWheel2()Dim p1(2) As Double, p2(2) As Double, p3(2) As Double, p4(2) As Double, p5(2) As Double, p6(2) As Double, p7(2) As Double, p8(2) As Double, p9(2) As Double, p10(2) As Double Dim Ln(10) As AcadLineDim Line As AcadEntityDim hatch1 As AcadHatchDim hatch2 As AcadHatchDim outerloop1(17) As AcadEntityDim outerloop2(9) As AcadEntityDim outerloop3(9) As AcadEntityGetVWheelDataIf D * 2 < VB ThenMsgBox "指定中心孔直径过小", vbOKOnly, "警告"Exit SubEnd IfUser_CreateLayersThisDrawing.ActiveLayer = yers.Item("粗实线层")pt0(0) = 2: pt0(1) = ddi / 2 + Vha: pt0(2) = 0'绘制第一个外边界Set Line = drawV(pt0(0) + Vf, pt0(1), beltz, Vphii)Set outerloop1(0) = Linep1(0) = pt1(0): p1(1) = pt1(1): p1(2) = pt1(2)Set Line = ThisDrawing.ModelSpace.AddLine(p1, pt0)Set outerloop1(1) = Linep2(0) = pt2(0): p2(1) = pt2(1): p2(2) = pt2(2)p1(0) = pt0(0) + VB: p1(1) = pt0(1): p1(2) = pt0(2)Set Line = ThisDrawing.ModelSpace.AddLine(p1, p2)Set outerloop1(2) = Linep2(0) = p1(0): p2(1) = p1(1) - Vha - Vhf - Vdelta + 2Set Line = ThisDrawing.ModelSpace.AddLine(p1, p2)Set outerloop1(3) = Linep8(0) = p2(0) - 2: p8(1) = p2(1) - 2Set Line = ThisDrawing.ModelSpace.AddLine(p2, p8)Set outerloop1(4) = Linep7(0) = pt0(0) + VB - VB * 3 / 8: p7(1) = p8(1) - (VB * 3 / 8 - 2) / 25 Set Line = ThisDrawing.ModelSpace.AddLine(p7, p8)Set outerloop1(5) = Linep6(0) = p7(0): p6(1) = 0.9 * D + (VB * 3 / 8 + 2) / 25:Set Line = ThisDrawing.ModelSpace.AddLine(p6, p7)Set outerloop1(6) = Linep1(0) = pt0(0): p1(1) = p2(1)Set Line = ThisDrawing.ModelSpace.AddLine(p1, pt0)Set outerloop1(7) = Linep3(0) = p1(0) + 2: p3(1) = p1(1) - 2Set Line = ThisDrawing.ModelSpace.AddLine(p1, p3)Set outerloop1(8) = Linep1(0) = pt0(0) + VB * 3 / 8: p1(1) = p3(1) - (VB * 3 / 8 - 2) / 25 Set Line = ThisDrawing.ModelSpace.AddLine(p3, p1)Set outerloop1(9) = Linep4(0) = p1(0): p4(1) = 0.9 * D + (VB * 3 / 8 + 2) / 25:Set Line = ThisDrawing.ModelSpace.AddLine(p1, p4)Set outerloop1(10) = Linep3(0) = 0: p3(1) = 0.9 * DSet Line = ThisDrawing.ModelSpace.AddLine(p3, p4)Set outerloop1(11) = Linep10(0) = p3(0): p10(1) = p3(1)p5(0) = p3(0) + VB + 4: p5(1) = p3(1)Set Line = ThisDrawing.ModelSpace.AddLine(p5, p6)Set outerloop1(12) = Linep1(0) = 0: p1(1) = 2 + D / 2Set Line = ThisDrawing.ModelSpace.AddLine(p1, p3)Set outerloop1(13) = Linep2(0) = 2: p2(1) = D / 2Set Line = ThisDrawing.ModelSpace.AddLine(p1, p2)Set outerloop1(14) = Linep3(0) = p2(0) + VB: p3(1) = p2(1)Set Line = ThisDrawing.ModelSpace.AddLine(p2, p3)Set outerloop1(15) = Linep1(0) = p3(0) + 2: p1(1) = p3(1) + 2Set Line = ThisDrawing.ModelSpace.AddLine(p1, p3)Set outerloop1(16) = LineSet Line = ThisDrawing.ModelSpace.AddLine(p1, p5)Set outerloop1(17) = Line'绘制剖面线User_CreateLayersThisDrawing.ActiveLayer = yers.Item("剖面线层") Set hatch1 = ThisDrawing.ModelSpace.AddHatch(0, "ansi31", True) hatch1.AppendOuterLoop (outerloop1)hatch1.PatternScale = 1.5hatch1.Evaluate'绘制其余边界ThisDrawing.ActiveLayer = yers.Item("粗实线层")p4(0) = VB + 2: p4(1) = 0Set Line = ThisDrawing.ModelSpace.AddLine(p3, p4)Set outerloop2(0) = Linep2(0) = VB + 4: p2(1) = 0Set Line = ThisDrawing.ModelSpace.AddLine(p1, p2)Set outerloop2(1) = Linep4(0) = 2: p4(1) = D / 2p2(0) = 2: p2(1) = 0Set Line = ThisDrawing.ModelSpace.AddLine(p2, p4)Set outerloop2(2) = Line''p4(0) = 0: p4(1) = 0p2(0) = 0: p2(1) = 2 + D / 2Set Line = ThisDrawing.ModelSpace.AddLine(p2, p4)Set outerloop2(3) = Linep4(0) = p3(0) - 2: p4(1) = p5(1) + 4 / 25p2(0) = p8(0): p2(1) = p8(1)Set Line = ThisDrawing.ModelSpace.AddLine(p2, p4)Set outerloop2(4) = Linep1(0) = p4(0) - VB + 4: p1(1) = p4(1)p2(0) = p8(0) - VB + 4: p2(1) = p8(1)Set Line = ThisDrawing.ModelSpace.AddLine(p1, p2)Set outerloop2(5) = Line''p4(0) = p3(0): p4(1) = p5(1) + 2 / 15p1(0) = p3(0): p1(1) = p8(1) + 2Set Line = ThisDrawing.ModelSpace.AddLine(p1, p4)Set outerloop2(6) = Line'p2(0) = p4(0) - VB: p2(1) = p4(1)p3(0) = p1(0) - VB: p3(1) = p1(1)Set Line = ThisDrawing.ModelSpace.AddLine(p2, p3)Set outerloop2(7) = Linep1(0) = 0: p1(1) = 0p2(0) = 100: p2(1) = 0'图形镜像Dim i As Integer, j As Integer, k As Integer, q As Integer For i = 0 To 17outerloop1(i).Mirror p1, p2NextFor j = 0 To 7outerloop2(j).Mirror p1, p2NextDim hatch3 As AcadHatch, hatch4 As AcadHatchSet hatch3 = hatch1.Mirror(p1, p2)hatch3.PatternAngle = 0ThisDrawing.Regen TrueZoomExtents'绘制中心线ThisDrawing.ActiveLayer = yers.Item("中心线层")p1(0) = -10: p1(1) = 0p2(0) = 10 + VB: p2(1) = 0Set Line = ThisDrawing.ModelSpace.AddLine(p1, p2) '中心孔基准直径线p1(1) = ddi / 2: p2(1) = ddi / 2Set Line = ThisDrawing.ModelSpace.AddLine(p1, p2) '带轮上基准直径线p1(1) = -1 * ddi / 2: p2(1) = -1 * ddi / 2Set Line = ThisDrawing.ModelSpace.AddLine(p1, p2) '带轮下基准直径线'槽中心线For i = 0 To beltz - 1p1(0) = 2 + Vf + Ve * i: p1(1) = ddi / 2 + Vha + 5p2(0) = 2 + Vf + Ve * i: p2(1) = ddi / 2 - Vhf - 5Set Line = ThisDrawing.ModelSpace.AddLine(p1, p2)NextFor k = 0 To beltz - 1p1(0) = 2 + Vf + Ve * k: p1(1) = -(ddi / 2 + Vha + 5)p2(0) = 2 + Vf + Ve * k: p2(1) = -(ddi / 2 - Vhf - 5)Set Line = ThisDrawing.ModelSpace.AddLine(p1, p2)Nextp1(0) = pt0(0) + VB / 2: p1(1) = ddi / 2 + Vha + 5p2(0) = pt0(0) + VB / 2: p2(1) = -(ddi / 2 + Vha + 5)Set Line = ThisDrawing.ModelSpace.AddLine(p1, p2)'尺寸标注ThisDrawing.ActiveLayer = yers.Item("标注层")Dim dimLn As AcadDimAlignedp1(0) = 2 + VB: p1(1) = D / 2p2(0) = 2 + VB: p2(1) = -1 * D / 2p3(0) = 24 + VB: p3(1) = 0: p3(2) = 0Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3)dimLn.TextOverride = "%%c<>"p1(0) = 4 + VB: p1(1) = p10(1)p2(0) = 4 + VB: p2(1) = -p10(1)p3(0) = 34 + VB: p3(1) = 0: p3(2) = 0Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3)dimLn.TextOverride = "%%c<>"p1(0) = 4: p1(1) = ddi / 2 - Vhf - Vdeltap2(0) = 4: p2(1) = -p1(1)p3(0) = -10: p3(1) = 0: p3(2) = 0Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3)dimLn.TextOverride = "%%c<>"p1(0) = 0: p1(1) = ddi / 2p2(0) = 0: p2(1) = -1 * ddi / 2p3(0) = -20: p3(1) = 0: p3(2) = 0Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3)dimLn.TextOverride = "%%c<>"p1(0) = 0: p1(1) = ddi / 2 + Vhap2(0) = 0: p2(1) = -1 * ddi / 2 - Vhap3(0) = -30: p3(1) = 0: p3(2) = 0Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3)dimLn.TextOverride = "%%c<>"p1(0) = 2 + 3 * VB / 8: p1(1) = -(p5(1) + p7(1)) / 2 - 3p2(0) = 2 + 5 * VB / 8: p2(1) = p1(1)p3(0) = 10 + 5 * VB / 8: p3(1) = p1(1) + 5: p3(2) = 0Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3)'标注槽尺寸p1(0) = 2 + Vf - Vbd / 2: p1(1) = ddi / 2p2(0) = 2 + Vf + Vbd / 2: p2(1) = p1(1)p3(0) = 2 + Vf: p3(1) = p1(1) + Vha + 10Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3) '标注Vbdp1(0) = 2: p1(1) = ddi / 2 + Vhap2(0) = 2 + Vf: p2(1) = p1(1)p3(0) = 2 + Vf / 2: p3(1) = p1(1) + 20Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3)Dim ts As Stringts = "\A1;\H2;<>\H0.75x;\S+2^-1"dimLn.TextOverride = tsdimLn.Update '标注VfIf beltz > 1 Thenp1(0) = 2 + Vf: p1(1) = ddi / 2p2(0) = p1(0) + Ve: p2(1) = p1(1)p3(0) = (p1(0) + p2(0)) / 2: p3(1) = p1(1) + Vha + 20Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3)ts = "\A1;\H2;<>\H0.75x;%%p0.4"dimLn.TextOverride = tsdimLn.UpdateEnd If '标注Vep1(0) = 2 + Vf - Vbd / 2 + Tan(Vphii * 3.14 / 360) * Vhf: p1(1) = ddi / 2 - Vhfp2(0) = 2 + Vf + Vbd / 2 - Tan(Vphii * 3.14 / 360) * Vhf: p2(1) = p1(1)p3(0) = 2 + Vf: p3(1) = p1(1) - 25pt0(0) = 2 + Vf: pt0(1) = ddi / 2 - Vbd / (2 * Tan(Vphii * 3.14 / 360))ThisDrawing.ActiveLayer = yers.Item("虚线层")Set Line = ThisDrawing.ModelSpace.AddLine(pt0, p1)Set Line = ThisDrawing.ModelSpace.AddLine(pt0, p2)ThisDrawing.ActiveLayer = yers.Item("标注层")Call ThisDrawing.ModelSpace.AddDimAngular(pt0, p1, p2, p3) '标注Vphip1(0) = 2: p1(1) = -ddi / 2 - Vhap2(0) = 2 + VB: p2(1) = p1(1)p3(0) = 2 + VB / 2: p3(1) = p1(1) - 20Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3)p1(0) = 0: p1(1) = -0.9 * Dp2(0) = 4 + VB: p2(1) = p1(1)p3(0) = 2 + VB / 2: p3(1) = -ddi / 2 - Vha - 30Set dimLn = ThisDrawing.ModelSpace.AddDimAligned(p1, p2, p3)'粗糙度p1(0) = 2 + Vf - Vbd / 2 + Tan(Vphii * 3.14 / 360): p1(1) = ddi / 2 - 1Call cucaodu(p1(0), p1(1), 3.2, -(90 - Vphii / 2), 3)End Sub'绘制第三种结构带轮Public Sub DrawWheel3()Dim p1(2) As Double, p2(2) As Double, p3(2) As Double, p4(2) As Double, p5(2) As Double, p6(2) As Double, p7(2) As Double, p8(2) As DoubleDim L(10) As AcadLineDim Line As AcadEntityDim hatch1 As AcadHatchDim hatch2 As AcadHatchDim outerloop1(11) As AcadEntityDim outerloop2(9) As AcadEntityDim outerloop3(9) As AcadEntityIf D * 2 < VB ThenMsgBox "指定中心孔直径过小", vbOKOnly, "警告"Exit SubEnd IfUser_CreateLayersThisDrawing.ActiveLayer = yers.Item("粗实线层") pt0(0) = 2: pt0(1) = ddi / 2 + Vha: pt0(2) = 0'绘制第一个外边界Set Line = drawV(pt0(0) + Vf, pt0(1), beltz, Vphii)Set outerloop1(0) = Linep1(0) = pt1(0): p1(1) = pt1(1): p1(2) = pt1(2)Set Line = ThisDrawing.ModelSpace.AddLine(p1, pt0)Set outerloop1(1) = Linep2(0) = pt2(0): p2(1) = pt2(1): p2(2) = pt2(2)p1(0) = pt0(0) + VB: p1(1) = pt0(1): p1(2) = pt0(2)Set Line = ThisDrawing.ModelSpace.AddLine(p1, p2)Set outerloop1(2) = Linep2(0) = p1(0): p2(1) = p1(1) - Vha - Vhf - Vdelta + 2Set Line = ThisDrawing.ModelSpace.AddLine(p1, p2)Set outerloop1(3) = Linep1(0) = pt0(0): p1(1) = p2(1)Set Line = ThisDrawing.ModelSpace.AddLine(p1, pt0)Set outerloop1(4) = Linep3(0) = p1(0) + 2: p3(1) = p1(1) - 2Set Line = ThisDrawing.ModelSpace.AddLine(p1, p3)Set outerloop1(5) = Linep1(0) = pt0(0) + VB * 3 / 8: p1(1) = p3(1) - (VB * 3 / 8 - 2) / 25 Set Line = ThisDrawing.ModelSpace.AddLine(p3, p1)Set outerloop1(6) = Linep3(0) = p1(0): p3(1) = p1(1) - VB / 8Set Line = ThisDrawing.ModelSpace.AddLine(p1, p3)。
AutoCAD二次开发实验四
实验四用户交互、选择集及扩展属性1,用户交互1.1读入字符串代码如下///<summary>/// getstring()方法提示用户在command提示光标处输入一个字符串/// PromptStringOptions对象用来控制用户的输入及提示信息的显示方式/// PromptStringOptions的AllowSpaces 属性来控制是否可以输入空格,如为false,则输入空格就停止///</summary>[CommandMethod("GetStringFromUser")]public void GetStringFromUser(){Document acDoc = Application.DocumentManager.MdiActiveDocument;PromptStringOptions pStrOpts = new PromptStringOptions("\n输入块名: ");pStrOpts.AllowSpaces = true;PromptResult pProRes = acDoc.Editor.GetString(pStrOpts);//string pBlackName = pProRes.StringResult.Trim();Application.ShowAlertDialog("输入的块名是: " + pProRes.StringResult);}, 1.2读入Double型数据//读入double型数据[CommandMethod("GetDouble")]private void GetDouble(){Document acDoc = Application.DocumentManager.MdiActiveDocument;PromptDoubleOptions pRadiusOpts = new PromptDoubleOptions("\n输入圆半径);PromptDoubleResult pRadiusRes = acDoc.Editor.GetDouble(pRadiusOpts);double pRadius = pRadiusRes.Value;}1.3 读入Point//读入点[CommandMethod("GetPoint")]private void GetPoint(){Document acDoc = Application.DocumentManager.MdiActiveDocument;PromptPointOptions pFirstPointOption = new PromptPointOptions("\n输º?入¨?起e点Ì?:êo");PromptPointResult pFirstPointResult =acDoc.Editor.GetPoint(pFirstPointOption);if (pFirstPointResult.Status == PromptStatus.OK){Point3d pFirstPt = pFirstPointResult.Value;}else{return;}}1.4 打开dwg文件//打开并插入dwg文件[CommandMethod("insertdwg")]static public void insertdwg(){OpenFileDialog pOpenFileDialog = new OpenFileDialog("打开地形图", null, "dwg", "openfile", OpenFileDialog.OpenFileDialogFlags.DefaultIsFolder);pOpenFileDialog.ShowDialog();string fname = pOpenFileDialog.Filename;Document doc = Application.DocumentManager.MdiActiveDocument;Editor ed = doc.Editor;if (!File.Exists(fname))fname = HostApplicationServices.Current.FindFile(fname, doc.Database, FindFileHint.Default);using (Database db = new Database(false, false)){db.ReadDwgFile(fname, FileShare.ReadWrite, true, null);using (Transaction t = doc.TransactionManager.StartTransaction()){int n = stIndexOf("\\");ObjectId idBTR = doc.Database.Insert(fname.Substring(n + 1, fname.Length -n - 5), db, false);BlockTable bt = (BlockTable)t.GetObject(doc.Database.BlockTableId, OpenMode.ForWrite);BlockTableRecord btr =(BlockTableRecord)t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);using (BlockReference bref = new BlockReference(Point3d.Origin, idBTR)) {DBObjectCollection pbrefColl = new DBObjectCollection();bref.Explode(pbrefColl);int num = pbrefColl.Count;for (int i = 0; i < num; i++){btr.AppendEntity(pbrefColl[i] as Entity);t.AddNewlyCreatedDBObject(pbrefColl[i], true);}}mit();}}}2,选择集2.1获得PickFirst选择集代码如下://选择集[CommandMethod("PickFirst", ePickSet)]public void CheckForPickfirstSelection(){ // 获得当前文档Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;// 获得 PickFirst 选择集PromptSelectionResult acSSPrompt;acSSPrompt = acDocEd.SelectImplied();SelectionSet acSSet;// 如果提示状态是 OK,那么对象在命令启动前已经被选择了if (acSSPrompt.Status == PromptStatus.OK){acSSet = acSSPrompt.Value;Application.ShowAlertDialog("您选择到的对象个数为: " + acSSet.Count.ToString()); }else{ Application.ShowAlertDialog("您选择到的对象个数为: 0");}// 清除 PickFirst 选择集ObjectId[] idarrayEmpty = new ObjectId[0];acDocEd.SetImpliedSelection(idarrayEmpty);// 要求在图形区域中选择对象acSSPrompt = acDocEd.GetSelection();// 如果提示状态是 OK,对象就被选择了if (acSSPrompt.Status == PromptStatus.OK){acSSet = acSSPrompt.Value;Application.ShowAlertDialog("您移除的对象个数为:"+acSSet.Count.ToString()); }else{Application.ShowAlertDialog("您移除的对象个数为:0");}}2.2在图形区域中选择对象代码://在图形区域中选择对象[CommandMethod("SelectObjectsOnscreen")]public void SelectObjectsOnscreen(){// 获得当前文档和数据库Document acDoc = Application.DocumentManager.MdiActiveDocument;Database acCurDb = acDoc.Database;// 要求在图形区域中选择对象PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection();SelectionSet acSSet = null;// 如果提示状态是 OK,对象就被选择了if (acSSPrompt.Status == PromptStatus.OK){ acSSet = acSSPrompt.Value; }// 启动一个事务using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) {// 遍历选择集中的对象foreach (SelectedObject acSSObj in acSSet){// 检查以确定返回的 SelectedObject 对象是有效的if (acSSObj != null){// 以写的方式打开选择的对象Entity acEnt = acTrans.GetObject(acSSObj.ObjectId,OpenMode.ForWrite) as Entity;if (acEnt != null){// 修改对象的颜色为绿色acEnt.ColorIndex = 3;}}}// 保存新对象到数据库中mit();}}2.3添加或合并多个选择集→#region合并选择集[CommandMethod("MergeSelectionSets")]public static void MergeSelectionSets(){// 获得当前文档的编辑器Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;PromptSelectionResult acSSPrompt;acSSPrompt = acDocEd.GetSelection();SelectionSet acSSet1;ObjectIdCollection acObjIdColl = new ObjectIdCollection();// 如果提示状态是 OK,对象就被选择了if (acSSPrompt.Status == PromptStatus.OK){acSSet1 = acSSPrompt.Value;// 添加选择的对象到 ObjectIDCollection 中acObjIdColl = new ObjectIdCollection(acSSet1.GetObjectIds());}// 要求在图形区域中选择对象acSSPrompt = acDocEd.GetSelection();SelectionSet acSSet2;// 如果提示状态是 OK,对象就被选择了if (acSSPrompt.Status == PromptStatus.OK){acSSet2 = acSSPrompt.Value;// 检查 ObjectIDCollection 的大小,如果为 0 就重新初始化它if (acObjIdColl.Count == 0){acObjIdColl = new ObjectIdCollection(acSSet2.GetObjectIds()); }else{// 遍历第二个选择集foreach (ObjectId acObjId in acSSet2.GetObjectIds()){// 添加每个对象 ID 到 ObjectIDCollection 中acObjIdColl.Add(acObjId);}}}Application.ShowAlertDialog("Number of objects selected: "+acObjIdColl.Count.ToString());}#endregion定义选择集过滤规则#region使用过滤器创建选择集[CommandMethod("FilterSelectionSet")]public void FilterSelectionSet(){ // 获得当前文档的编辑器Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;// 创建一个 TypedValue 数组,用于定义过滤条件TypedValue[] acTypValAr = new TypedValue[1];acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "CIRCLE"), 0);// 赋值过滤条件给 SelectionFilter 对象SelectionFilter acSelFtr = new SelectionFilter(acTypValAr);// 要求在图形区域中选择对象PromptSelectionResult acSSPrompt;acSSPrompt = acDocEd.GetSelection(acSelFtr);// 如果提示状态是 OK,对象就被选择了if (acSSPrompt.Status == PromptStatus.OK){SelectionSet acSSet = acSSPrompt.Value;Application.ShowAlertDialog("所选择对象个数: " + acSSet.Count.ToString()); }else{Application.ShowAlertDialog("所选择对象个数:0");}}#endregion从选择集中移除对象// 清除 PickFirst 选择集ObjectId[] idarrayEmpty = new ObjectId[0];acDocEd.SetImpliedSelection(idarrayEmpty);// 要求在图形区域中选择对象acSSPrompt = acDocEd.GetSelection();// 如果提示状态是 OK,对象就被选择了if (acSSPrompt.Status == PromptStatus.OK){acSSet = acSSPrompt.Value;Application.ShowAlertDialog("您移除的对象个数为: "+acSSet.Count.ToString()); }else{Application.ShowAlertDialog("您移除的对象个数为: 0");}扩展属性// 创建拓展的过滤选择条件[CommandMethod("FilterXdata")]public void FilterXdata(){// 获得当前文档的编辑器Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;// 创建一个 TypedValue 数组,用于定义过滤条件TypedValue[] acTypValAr = new TypedValue[2];acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "Circle"), 0);acTypValAr.SetValue(new TypedValue((int)DxfCode.ExtendedDataRegAppName, "MY_APP"), 1);SelectionFilter acSelFtr = new SelectionFilter(acTypValAr);// 要求在图形区域中选择对象PromptSelectionResult acSSPrompt;acSSPrompt = acDocEd.GetSelection(acSelFtr);// 如果提示状态是 OK,对象就被选择了if (acSSPrompt.Status == PromptStatus.OK){ SelectionSet acSSet = acSSPrompt.Value;Application.ShowAlertDialog("您选择到的对象个数为: " +acSSet.Count.ToString());}else{ Application.ShowAlertDialog("您选择到的对象个数为: 0");}}。
CAD二次实验报告4
实验四用户交互、选择集及扩展属性一、主要内容1、基于用户交互式的信息输入2、创建选择集3、给图形实体添加扩展属性二、学时安排三、预习内容回顾用户交互命令的使用、选择集创建的几种方式,四、实验步骤4.1 用户交互Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;//提示输入圆心坐标PromptPointOptions fristpoint = new PromptPointOptions("\n请输入圆心:");……//提示输入半径PromptDoubleOptions r = new PromptDoubleOptions("\n请输入圆半径:");……4.1.1 读入字符串//获取提示框输入的或点选的坐标PromptPointResult result = ed.GetPoint(fristpoint);Point3d p = result.Value;4.1.2 读入Double型数据//获取输入半径,并转换为double类型PromptDoubleResult Temp_radius = ed.GetDouble(r);//通过PromptDoubleResult下的属性Value转换为.NET下的Double类型double radius = Temp_radius.Value;4.1.3 读入Point//声明圆对象Circle c = new Circle();c.Center = p;c.Radius = radius;4.1.4 拾取实体Entity ent = (Entity)acTrans.GetObject(id, OpenMode.ForRead, false);4.1.5 打开dwg文件//打开并插入dwg文件[CommandMethod("insertdwg")]static public void insertdwg(){OpenFileDialog pOpenFileDialog = new OpenFileDialog("打开地形图", null, "dwg", "openfile", OpenFileDialog.OpenFileDialogFlags.DefaultIsFolder);pOpenFileDialog.ShowDialog();string fname = pOpenFileDialog.Filename;Document doc = Application.DocumentManager.MdiActiveDocument;Editor ed = doc.Editor;if (!File.Exists(fname))fname = HostApplicationServices.Current.FindFile(fname, doc.Database, FindFileHint.Default);using (Database db = new Database(false, false)){db.ReadDwgFile(fname, FileShare.ReadWrite, true, null);using (Transaction t = doc.TransactionManager.StartTransaction()){int n = stIndexOf("\\");ObjectId idBTR = doc.Database.Insert(fname.Substring(n + 1, fname.Length - n - 5), db, false);BlockTable bt = (BlockTable)t.GetObject(doc.Database.BlockTableId, OpenMode.ForWrite);BlockTableRecord btr =(BlockTableRecord)t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);using (BlockReference bref = new BlockReference(Point3d.Origin, idBTR)) {DBObjectCollection pbrefColl = new DBObjectCollection();bref.Explode(pbrefColl);int num = pbrefColl.Count;for (int i = 0; i < num; i++){btr.AppendEntity(pbrefColl[i] as Entity);t.AddNewlyCreatedDBObject(pbrefColl[i], true);}}mit();}}}4.2 选择集4.2.1 获得PickFirst选择集//选择集[CommandMethod("PickFirst", ePickSet)]public void CheckForPickfirstSelection(){ // 获得当前文档Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;// 获得 PickFirst 选择集PromptSelectionResult acSSPrompt;acSSPrompt = acDocEd.SelectImplied();SelectionSet acSSet;// 如果提示状态是 OK,那么对象在命令启动前已经被选择了if (acSSPrompt.Status == PromptStatus.OK){acSSet = acSSPrompt.Value;Application.ShowAlertDialog("您选择到的对象个数为: " + acSSet.Count.ToString()); }else{ Application.ShowAlertDialog("您选择到的对象个数为: 0");}// 清除 PickFirst 选择集ObjectId[] idarrayEmpty = new ObjectId[0];acDocEd.SetImpliedSelection(idarrayEmpty);// 要求在图形区域中选择对象acSSPrompt = acDocEd.GetSelection();// 如果提示状态是 OK,对象就被选择了if (acSSPrompt.Status == PromptStatus.OK){acSSet = acSSPrompt.Value;Application.ShowAlertDialog("您移除的对象个数为:"+acSSet.Count.ToString()); }else{Application.ShowAlertDialog("您移除的对象个数为:0");}}4.2.2 在图形区域中选择对象//在图形区域中选择对象[CommandMethod("SelectObjectsOnscreen")]public void SelectObjectsOnscreen(){// 获得当前文档和数据库Document acDoc = Application.DocumentManager.MdiActiveDocument;Database acCurDb = acDoc.Database;// 要求在图形区域中选择对象PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection();SelectionSet acSSet = null;// 如果提示状态是 OK,对象就被选择了if (acSSPrompt.Status == PromptStatus.OK){ acSSet = acSSPrompt.Value; }// 启动一个事务using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) {// 遍历选择集中的对象foreach (SelectedObject acSSObj in acSSet){// 检查以确定返回的 SelectedObject 对象是有效的if (acSSObj != null){// 以写的方式打开选择的对象Entity acEnt = acTrans.GetObject(acSSObj.ObjectId,OpenMode.ForWrite) as Entity;if (acEnt != null){// 修改对象的颜色为绿色acEnt.ColorIndex = 3;}}}// 保存新对象到数据库中mit();}}4.2.3 添加或合并多个选择集#region合并选择集[CommandMethod("MergeSelectionSets")]public static void MergeSelectionSets(){// 获得当前文档的编辑器Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;PromptSelectionResult acSSPrompt;acSSPrompt = acDocEd.GetSelection();SelectionSet acSSet1;ObjectIdCollection acObjIdColl = new ObjectIdCollection();// 如果提示状态是 OK,对象就被选择了if (acSSPrompt.Status == PromptStatus.OK){acSSet1 = acSSPrompt.Value;// 添加选择的对象到 ObjectIDCollection 中acObjIdColl = new ObjectIdCollection(acSSet1.GetObjectIds());}// 要求在图形区域中选择对象acSSPrompt = acDocEd.GetSelection();SelectionSet acSSet2;// 如果提示状态是 OK,对象就被选择了if (acSSPrompt.Status == PromptStatus.OK){acSSet2 = acSSPrompt.Value;// 检查 ObjectIDCollection 的大小,如果为 0 就重新初始化它if (acObjIdColl.Count == 0){acObjIdColl = new ObjectIdCollection(acSSet2.GetObjectIds()); }else{// 遍历第二个选择集foreach (ObjectId acObjId in acSSet2.GetObjectIds()){// 添加每个对象 ID 到 ObjectIDCollection 中acObjIdColl.Add(acObjId);}}}Application.ShowAlertDialog("Number of objects selected: "+acObjIdColl.Count.ToString());}#endregion4.2.4 定义选择集过滤规则#region使用过滤器创建选择集[CommandMethod("FilterSelectionSet")]public void FilterSelectionSet(){ // 获得当前文档的编辑器Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;// 创建一个 TypedValue 数组,用于定义过滤条件TypedValue[] acTypValAr = new TypedValue[1];acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "CIRCLE"), 0);// 赋值过滤条件给 SelectionFilter 对象SelectionFilter acSelFtr = new SelectionFilter(acTypValAr);// 要求在图形区域中选择对象PromptSelectionResult acSSPrompt;acSSPrompt = acDocEd.GetSelection(acSelFtr);// 如果提示状态是 OK,对象就被选择了if (acSSPrompt.Status == PromptStatus.OK){SelectionSet acSSet = acSSPrompt.Value;Application.ShowAlertDialog("所选择对象个数: " + acSSet.Count.ToString()); }else{Application.ShowAlertDialog("所选择对象个数:0");}}#endregion4.2.5 从选择集中移除对象// 清除 PickFirst 选择集ObjectId[] idarrayEmpty = new ObjectId[0];acDocEd.SetImpliedSelection(idarrayEmpty);// 要求在图形区域中选择对象acSSPrompt = acDocEd.GetSelection();// 如果提示状态是 OK,对象就被选择了if (acSSPrompt.Status == PromptStatus.OK){acSSet = acSSPrompt.Value;Application.ShowAlertDialog("您移除的对象个数为: "+acSSet.Count.ToString()); }else{Application.ShowAlertDialog("您移除的对象个数为: 0");}4.3 扩展属性#region创建扩展属性过滤器建立选择集[CommandMethod("FilterXdata")]public void FilterXdata(){// 获得当前文档的编辑器Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;// 创建一个 TypedValue 数组,用于定义过滤条件TypedValue[] acTypValAr = new TypedValue[2];acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "Circle"), 0);acTypValAr.SetValue(new TypedValue((int)DxfCode.ExtendedDataRegAppName, "MY_APP"), 1);SelectionFilter acSelFtr = new SelectionFilter(acTypValAr);// 要求在图形区域中选择对象PromptSelectionResult acSSPrompt;acSSPrompt = acDocEd.GetSelection(acSelFtr);// 如果提示状态是 OK,对象就被选择了if (acSSPrompt.Status == PromptStatus.OK){ SelectionSet acSSet = acSSPrompt.Value;Application.ShowAlertDialog("您选择到的对象个数为: " +acSSet.Count.ToString());}else{ Application.ShowAlertDialog("您选择到的对象个数为: 0");}}#endregion。
AutoCAD二次开发实验二
pc.Add(p1);
pc.Add(p2);
pc.Add(p3);
pc.Add(p4);
pc.Add(p5);
Spline spline = new Spline(pc, 2, 5);
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
curveSegments.Add(entity);
regions =Region.CreateFromCurves(curveSegments);
RegionnewRegion = (Region)regions[0];
newRegion.SetDatabaseDefaults();
BlockTablebt = (BlockTable)Trans.GetObject(db.BlockTableId,OpenMode.ForRead);
using (Transaction trans = db.TransactionManager.StartTransaction())
{
Ellipse pEllipse = new Ellipse(new Point3d(150, 150, 0), new Vector3d(0, 0, 50), new Vector3d(50, 50, 0), 0.8, 0, 0);
BlockTableRecordc_block_record = (BlockTableRecord)trans.GetObject(c_block[BlockTableRecord.ModelSpace],OpenMode.ForWrite);//写入,1
CAD在工程实践中的二次开发开题报告
5)在SQL程序里新建一个数据库,并在数据库里新建两个表。学习使用ADO管理数据库。
6)在VBA中创建模块、窗体并实现对平面数据、地质编录数据的管理操作。
7)用坐标法计算主点里程、方位角最终Biblioteka 现在窗体中计算任一里程的方位角的目的。
在AutoCAD中实现VBA有四大优点:
(1) Visual Basic编程环境易于学习和使用。
(2)VBA可与AutoCAD在同一进程空间中运行,这使程序执行得非常快。
(3)对话框的构造快速而有效。这使开发人员可以构造原型应用程序并迅速收到设计的反馈。
(4)工程可以是独立的,也可以嵌入到图形中。这样就为开发人员提供了非常灵活的方式来发布他们的应用程序。
8)自动绘图及报表模块设计、实现,控制模块设计、实现。
3、软件开发步骤:
1)软件规划阶段
主要任务是确定将开发的软件“做什么”,提出开发任务,在经济上技术上是否可行。
2)软件需求分析阶段
主要任务任然是解决“做什么”的问题,它把规划阶段初步拟定的软件工作范围进行求精很细化,确定用户要求和软件模式,确定软件功能,分析各种课能的解法,构造出系统的数据和数据结构,配置各个软件元素。
本设计通过最近几年的计算机辅助设计和绘图工作中应用AutoCAD进行二次开发的一些经验知识,来说明AutoCAD二次开发技术在工程图纸绘制中的应用。
三、主要工作和采用的方法、手段
1、主要工作和内容:
通过对AutoCAD平台的二次开发,完成“铁路隧道地质编录自动成图系统”开发。
1)线路平面数据管理模块设计、实现。
2)地质编录数据管理模块设计、实现。
AutoCAD二次开发技术及其应用研究
AutoCAD二次开发技术及其应用研究引言AutoCAD作为一种通用计算机辅助设计软件,其二维图形设计功能非常强大,目前已广泛应用于机械、电子、电气、建筑、土木、服装等领域,深受国内外众多工程设计人员的喜爱,成为目前世界上应用最广的计算机辅助设计工具。
尽管如此,人们在使用过程中还是发现其存在较多的不足之处,如对于一些特殊曲线的绘制往往较为困难,而对于一些标准件、常用件,虽然从结构上看它们较为相似,但却难以采用AutoCAD提供的复制、镜像、旋转等命令来实现,而必须一笔划一笔划的绘制,显得费时费力。
好在AutoDesk公司在开发AutoCAD之初,就意识到上述不足,及时提供了二次开发语言AutoLISP,并且随着软件技术的发展,不断加以更新,先后推出了ADS、ObjectARX、VBA、VisualLISP、DotNet等二次开发语言,适合于各种不同类型设计人员对AutoCAD进行二次开发。
一、 AutoCAD二次开发语言1.1 AutoLISPAutoLISP最早出现在AutoCAD R2.17中,是一种嵌入在AutoCAD 内部的二次开发语言,它的全名为表处理语言(usT Processing Language),来源于人工智能语言LISP,是被解释执行的,任何一个语句键入后就能马上执行。
由于使用方便,其一直是低版本AutoCAD 的首选编程语言。
随着Windows界面技术的发展,从AutoCAD R12.0开始支持对话框设计语言DCL,用于编制友好的图形用户界面。
不过由于代码保密性能不好等因素,目前已被VLISP编程语言所代替。
1.2 ADSADS的全名是AutoCAD Development System。
它出现于AutoCAD R11.0中,为那些学习C语言的工程设计人员提供了一种二次开发环境。
ADS本质上是一组可以用编写AutoCAD应用程序的头文件和目标库,它直接利用用户熟悉的各种流行的c语言编译器,将应用程序编译成可执行的文件在AutoCAD环境下运行。
CAD在工程实践中的二次开发
摘要为了解决困扰工程地质界的隧道地质编录绘图难题,探索实现隧洞设计及施工地质展示图绘图自动化和计算机化的可能途径,本文以田坝岭隧道施工地质为范例,说明铁路隧道地质编录自动成图系统概念和实施要点,通过对AutoCAD平台的二次开发最终实现自动成图的目的。
通过本设计过程,使学生掌握计算机辅助设计的基本技能,并具备AutoCAD二次开发的初步技能,提高其应用计算机解决实际问题的能力水平,预期达到以下要求:熟练掌握AutoCAD平台Axtivex 开发技术;初步具备利用关系型数据库管理工程数据的能力。
本设计可以实现加入窗体模块编程,每确认一个窗体模块即可实现一项模块功能。
使工作量由软件从已编写的数据库文件中提取数据自动生成图形。
用铁路隧道地质编录自动成图系统绘制隧道展示图比手工快几十倍甚至更多。
关键词:地质展示图数据库二次开发AbstractIn order to solve the trouble DeZhiJie tunnel engineering geological record drawing problem, explore and realize tunnel in the design and construction of geological display figure drawing automation and computerized possible approach, based on the tunnel construction geological field dam as an example, explain railway tunnel geological record to be automatic mapping system concept and the key points, realized through to the second development AutoCAD platform eventually realize the purpose of automatic mapping.Through the design process, make the students master the basic skills of computer aided design, and with AutoCAD secondary development preliminary skills, improve its application computer solution actual problem ability level, expected to reach the following requirements: master AutoCAD platform development technology Axtivex; Initially have use of relational database management engineering data ability.This design can realize to join form module programming, each confirmed a form module can be realized a module function. Make work from already written by software database file extract data automatically generate graphics. Use railway tunnel geological record to be automatic mapping system draw tunnel show than a few times fast figure by even more.Key word :Geological display figure database Second development目录第 1 章绪论 (1)1.1开发的意义、背景 (1)1.2本设计解决的问题 (1)1.3采用的方法和步骤 (2)1.3.1 方法 (2)1.3.2 步骤 (2)1.4预期成果 (3)第 2 章数据库结构设计 (4)2.1数据库中的表 (4)2.2数据库中表的关联图 (7)第 3 章程序结构 (9)3.1模块 (9)3.1.1 Caidan 模块 (9)3.1.2 Caidanxianshi 模块 (9)3.1.3 CommASS 模块 (10)3.1.4 DZSub 模块 (10)3.2窗体 (10)3.2.1 工程管理窗体 (10)3.2.2 工程数据管理窗体 (11)3.2.3 平面数据管理 (12)3.2.4 岩性表的管理 (13)3.2.5 编录出图 (13)3.2.6 打印 (14)第 4 章重点算法 (15)4.1平面线路方位角的计算 (15)4.1.1 任意里程的方位角计算 (15)4.1.2 线路走向计算 (16)第 5 章主要应用技术 (19)5.1ADO技术简介 (19)5.1.1 Connection 连接对象 (19)5.1.2 Recordset对象 (20)5.2A UTO CAD VBA简介 (23)5.2.1 什么是AutoCAD VBA (23)5.2.2 用VBA 管理器组织工程 (24)5.2.3 处理宏 (25)5.2.4 VBA IDE 编辑工程 (26)第 6 章工程实例 (28)6.1工程名称的录入 (28)6.2岩性表设置 (29)6.3平面数据录入 (29)6.4工程地质数据录入 (31)6.5编录出图 (37)第7 章结论 (39)7.1最终成果 (39)7.2总结 (39)7.3展望 (39)参考文献 (40)致谢 (41)附录A 外文资料翻译 (42)附录B 代码 (56)第 1 章绪论1.1 开发的意义、背景为了解决困扰工程地质界的隧道地质编录绘图难题,探索实现隧洞设计及施工地质展示图绘图自动化和计算机化的可能途径,本文以田坝岭隧道施工地质为范例,提出了铁路隧道地质编录自动成图系统概念和实施要点,实现了通过对AutoCAD平台的二次开发最终实现自动成图的目的。
CAD二次开发总结
2.AutoLISP语言
Visual LISP,对AutoLISP是一次重大改进,增 强了 AutoCAD的二次开发能力。可使用户方便地用 AutoLISP语言进行程序开发中的编写程序代码、调 试程序等操作。
2005-09-10 第三部分 AutoCAD二次开发 3
7.2 AutoLISP程序设计基础
(max <数1> <数2>)返回<数1><数2>…中的最大值。
(min <数1> <数2>)返回<数1><数2>…中的最小值。
(exp <数>)
返回e的<数>次幂的值。
(expt <底数> <幂>)返回<底数>的<幂>次方的值。
(log <数> )
2005-09-10
是exp函数的反函数。返回值为<数>的自
规则1:以括号组成表达式,左右括号“(”&“)”一定要配对。
(函数 [参数表])
即由一个函数名和调用这个函数所需的参数表组成。参数表 中的参数也可以是表达式,即AutoLISP的表达式可以嵌套。
2005-09-10 第三部分 AutoCAD二次开发 4
规则2:注释以分号“;”开头,它可以单独位于一行, 也可以位于某一程序语句的后面。 编写 AutoLISP 程序时,应添加必要的注释,以说明 程序的功能、编写时间、作者、某些程序代码的具体含 义等信息,这些信息将有助于程序的编写与修改。 规则3:“前缀表示法”。 AutoLISP 语言的结构与常用高级结构有所区别。像 BASIC 、 PASCAL 、 C 等语言均采用“中缀表示法”。 当给变量a赋值12.0时,通常为下面的形式: a=12.0; 即赋值运算符“=”位于中间。 AutoLISP 则采用,即把运算符放在参数表的第一个 元素的位置,后面为其参数。 例如,通常用到的表达式X=(A+B)×C在AutoLISP中 表示为:(setq X (* (+ A B) C))
CAD实验--图形软件的二次开发
学生实验报告书
实验课程名称CAD/CAM基础
开课学院
指导教师姓名
学生姓名
学生专业班级
-- 学年第学期
实验教学管理基本规范
实验是培养学生动手能力、分析解决问题能力的重要环节;实验报告是反映实验教学水平与质量的重要依据。
为加强实验过程管理,改革实验成绩考核方法,改善实验教学效果,提高学生质量,特制定实验教学管理基本规范。
1、本规范适用于理工科类专业实验课程,文、经、管、计算机类实验课程可根据具体情况参
照执行或暂不执行。
2、每门实验课程一般会包括许多实验项目,除非常简单的验证演示性实验项目可以不写实验
报告外,其他实验项目均应按本格式完成实验报告。
3、实验报告应由实验预习、实验过程、结果分析三大部分组成。
每部分均在实验成绩中占一
定比例。
各部分成绩的观测点、考核目标、所占比例可参考附表执行。
各专业也可以根据具体情况,调整考核内容和评分标准。
4、学生必须在完成实验预习内容的前提下进行实验。
教师要在实验过程中抽查学生预习情况,
在学生离开实验室前,检查学生实验操作和记录情况,并在实验报告第二部分教师签字栏签名,以确保实验记录的真实性。
5、教师应及时评阅学生的实验报告并给出各实验项目成绩,完整保存实验报告。
在完成所有
实验项目后,教师应按学生姓名将批改好的各实验项目实验报告装订成册,构成该实验课程总报告,按班级交课程承担单位(实验中心或实验室)保管存档。
6、实验课程成绩按其类型采取百分制或优、良、中、及格和不及格五级评定。
实验课程名称:___CAD/CAM基础____________。
CAD二次开发报告示例
城市轨道交通线路设计平曲线绘制一、程序要求1、新建数据库,名称为“基础数据.mdb”。
2、建立数据表:plane_CureveData(平面曲线信息表)。
字段名称和类型如表1所示。
3、同时,新建一个窗体,窗体中放置MsFLexGrid表格控件一个,导入按钮实现将文本文件内容导入到MsFLexGrid表格控件;增加、修改、删除数据的相关编辑;保存实现表格数据保存到数据库(基础数据.mdb)中功能,界面如图1。
4、根据曲线信息,计算五大桩里程,新建立图层“平曲线图”,并在图层“平曲线图”中绘制平曲线图,并标注出曲线五要素,字体任意设定为中文字体,颜色任意。
同时设定线宽为1、线颜色任意。
表1 平面曲线信息表字段名称和类型字段名称数据类型交点编号 dbText交点桩号 dbSingle曲线类型 dbText曲线转向 dbText曲线半径 dbSingle曲线转角 dbSingle起端缓和曲线长度 dbSingle终端缓和曲线长度 dbSingle二、主要过程1、新建三个窗体,分别为新建项目、打开项目和平面曲线数据。
“新建项目”窗体实现新建名为“基础数据.xxd”的项目文件和“基础数据.mdb”的数据库文件(内含数据表plane_CureveData,表内无数据);“打开项目”窗体实现打开已有项目并在AutoCAD 中自动绘制平曲线图;“平面曲线数据”窗体包含一个FlexGrid 表格控件,实现导入包含平面曲线数据的文本文件(平面曲线.txt)并且能够对表格内的数据进行增加、修改、删除,最终保存入已打开项目对应的数据库文件中。
2、在程序中,按照上述要求编写相关代码。
节选部分重要代码如下所示:(1)新建数据库代码图1 平面曲线数据窗体Public Sub CreateDatabase() '创建数据库Dim m_WorkSpace As WorkspaceSet m_WorkSpace = DBEngine.Workspaces(0)Dim newdb As Database '定义数据库变量m_DatabaseName = m_ProjectPath + "\基础数据.mdb" '设定新建数据库位置Set newdb = m_WorkSpace.CreateDatabase(m_DatabaseName, dbLangChineseSimplified)Dim newtbl As TableDef'创建表 plane_CureveDataSet newtbl = newdb.CreateTableDef("plane_CureveData")'平面曲线信息表Set m_Field(1) = newtbl.CreateField("交点编号", dbText)Set m_Field(2) = newtbl.CreateField("交点桩号",dbSingle)Set m_Field(3) = newtbl.CreateField("曲线类型", dbText)Set m_Field(4) = newtbl.CreateField("曲线转向", dbText)Set m_Field(5) = newtbl.CreateField("曲线半径", dbSingle)Set m_Field(6) = newtbl.CreateField("曲线转角", dbSingle)Set m_Field(7) = newtbl.CreateField("起端缓和曲线长度", dbSingle)Set m_Field(8) = newtbl.CreateField("终端缓和曲线长度", dbSingle)Call CreateTable(newdb, newtbl, 8) '生成表MsgBox "项目创建成功", vbInformation, "新建项目"newdb.Close '关闭数据库及其中的表Set newdb = Nothing '删除数据库对象实例End Sub(2)创建菜单示例创建项目管理菜单Public Function CreateProjectManageMenu()On Error Resume NextDim mnuGroup As AcadMenuGroupDim mnuMain As AcadPopupMenuDim menuNames As StringDim menuCollection As AcadPopupMenusDim menu As AcadPopupMenuSet mnuGroup=ThisDrawing.Application.MenuGroups.Item(0) Set mnuMain = mnuGroup.Menus.Add("项目管理(&P)")Call BuildSubMenu(mnuMain, "新建设计项目(&N)", "mcoNewProject")'分隔符End Function(3)绘制平曲线代码'绘制平面线型Sub DrawPMLine()On Error Resume NextDim plineObj As AcadLWPolylineDim points(0 To 11) As DoubleDim textString As StringDim QZ As Double '曲中桩号Dim ZH As Double '直缓桩号Dim HY As Double '缓圆桩号Dim YH As Double '园缓桩号Dim HZ As Double '缓直桩号Dim a As Double '转角Dim Ls0 As Double '起端缓和曲线长Dim Le0 As Double '终端缓和曲线长Dim R As Double '曲线半径Dim L As Double '曲线长Dim T As Double '切线长Dim P As DoubleDim m As DoubleDim PI As DoubleDim Text_a As StringDim a_Du As DoubleDim a_Fen As DoubleDim a_Miao As DoubleCall SetFontName("楷体_GB2312")PI = 3.14159265359If Len(m_DatabaseName) <= 0 Then '表示还没有打开数据库连接Exit SubEnd If' 创建名为"平曲线图"的新图层Dim LayerObj As AcadLayerSet LayerObj = yers.Add("平曲线图")LayerObj.color = acCyan ' 指定"平曲线图"图层的颜色为青色Dim i As LongDim mSQL As String'打开数据库连接Set Con = New ADODB.ConnectionmSQL = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + m_DatabaseName + ";"Con.Open mSQL'设置记录集Set mrs = New ADODB.RecordsetSet mrs.ActiveConnection = Con '设置记录当前接连mrs.CursorLocation = adUseClientmSQL = "select * from plane_CureveData ORDER BY 交点桩号 ASC"mrs.Open mSQL, Con, adOpenKeyset, adLockOptimistic '打开记录集Dim m_Totals As Integerm_Totals = 0Do While Not mrs.EOF'将表格所有数据写入数据库表中m_Totals = m_Totals + 1If (m_Totals = 1) Thena = mrs.Fields(5)Ls0 = mrs.Fields(6)Le0 = mrs.Fields(7)R = mrs.Fields(4)P = Ls0 * Ls0 / (24 * R)m = Ls0 / 2T = (R + P) * Tan(a * PI / 360) + mL = PI * a * R / 180 + Ls0a_Du = Fix(a)a_Fen = Fix((a - a_Du) * 60)a_Miao = Round(((a - a_Du) * 60 - a_Fen) * 60, 2)Text_a = Str(a_Du) & "°" & Str(a_Fen) & "′" & Str(a_Miao) & "″"textString = "R=" & Str(R) & " " & "α=" & Text_a & " " & vbCrLf & "T=" & Str(Round(T, 2)) & " " & "lo=" & Str(Ls0) + " " & "L=" & Str(Round(L, 2))QZ = mrs.Fields(1)ZH = QZ - THY = ZH + Ls0HZ = QZ + TYH = HZ - Le0points(0) = 0: points(1) = 0points(2) = ZH: points(3) = points(1)points(4) = HYIf (mrs.Fields(3) = "左") Thenpoints(5) = -50Call DrawText(ZH + 30, points(1) + 80, textString, 150, 0, acAlignmentMiddleLeft)ElseIf (mrs.Fields(3) = "右") Thenpoints(5) = 50Call DrawText(ZH + 30, points(1) - 30, textString, 150, 0, acAlignmentMiddleLeft)End IfEnd Ifpoints(6) = QZ: points(7) = points(5)points(8) = YH: points(9) = points(5)points(10) = HZ: points(11) = points(1)m_X = points(10): m_Y = points(11)Set plineObj=ThisDrawing.ModelSpace. _ AddLightWeightPolyline(points)yer = "平曲线图"plineObj.ConstantWidth = 1 '多段线全局宽度 m_Totals = m_Totals + 1mrs.MoveNextEnd If(以上是第一段曲线绘制的代码)plineObj.ConstantWidth = 1 '多段线全局宽度'' 指定多段线到"地面线"图层上yer = "平曲线图"plineObj.UpdateZoomAllEnd Sub3、效果展示图2 第一段平曲线图。
关于Autocad二次开发的研究
关于Autocad二次开发的研究,基于Activex科技在有轨运输铁路线上的应用摘要:本文介绍了在Autocad二次开发的中所使用的工具。
在对ActiveX自动科技和Visual Basic进行比较之后,展示了为什么两者被选为开发方法,并给出了源代码。
这些源代码与数据库、Visual Basic和AutoCad之间的通信、关于AutoCAD的主体特点的提取以及释义有关。
这些应用程序很好地用户界面并且具有功能像项目管理、定制以及数据库的操作等等。
项目管理采用了不同的数据结构,这些数据结构主要是建立在不同的线路设计的不同阶段之上的,例如如可行性研究和有线运输的设计研究。
用户化反映了AutoCAD绘图的不同要求。
数据库的操作正在改变、添加、删除、再生、分类以及数据表的统计和记录。
这种软件实现了AutoCAD操作和Access数据库之间的无缝连接。
这给AutoCAD的主体特点带来的改变将会实时地影响数据库的记录。
与之相反,如果数据库已经发生变化,那么对于手工劳动而进行的AutoCAD的有轨运输来说,绘制线条就毫无必要。
这一软件能够自动地根据跟新的数据进行提取,它也展示了一些前期工作,这些前期工作是协调紧接着要进行的工作的前提。
经过北京城市铁路的应用,证明了本研究的可行性。
它减少了由人工干预而产生的错误,节省了设计的时间、促进了以后的行业并且加速了整个交通运输项目的进程。
如果本研究在其他相似行业能得到应用,将会有更广泛的应用前景。
研究背景:随着城市的飞速发展,必须得解决城市的交通问题。
中国是一个发展中国家,人口众多。
随着私家车数量的愈发增多,由机动车所引起的污染问题也变得越来越糟糕。
城市轨道交通因为速度快,污染和噪音少等特点,发展成为一种解决污染问题有效方法。
在过去的几十年里,中国已经有超过三十城市计划发展城市轨道交通。
北京、上海、天津、广州、香港拥有超过193.3千米长的城市轨道交通。
城市轨道交通的前景大好。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
目录实验一 ObjectARX .NET托管开发包的使用 (2)一、主要内容 (2)二、学时安排 (2)三、预习内容及要求 (2)四、下载安装ObjectARX (2)4.1下载ObjectARX (2)4.2 安装ObjectARX向导和托管项目模板 (2)4.3 Hello World程序创建 (3)4.4 创建上下文菜单 (6)4.5 创建弹出式下拉菜单 (7)4.6 创建工具条 (10)4.7 创建有模态和无模态对话框 (13)4.8 修改状态栏 (17)4.9 创建进度条 (18)实验二 AutoCAD基本图形对象创建与编辑 (20)一、主要内容 (20)二、学时安排 (20)三、预习内容 (20)四、基本图形对象创建与编辑 (20)基本图形对象创建:添加引用 (20)4.1 创建直线对象 (21)4.2 创建圆 (22)4.3 创建圆弧 (23)4.4 创建多段线 (24)4.5 创建椭圆 (27)4.6 样条曲线 (28)4.7 创建面域 (29)4.8 创建文字 (32)4.9 创建填充 (34)4.10 创建尺寸标注 (35)实验三 ObjectARX .NET托管开发包的使用 (42)一、主要内容 (42)二、学时安排 (42)三、预习内容 (42)四、ObjectARX .NET托管开发包的使用过程 (42)4.1创建块定义及插入块参照 (42)4.1.1 创建块定义 (43)4.1.2 插入块参照 (44)4.2创建带有属性的块定义及插入带有属性的块参照 (45)4.2.1 创建带有属性的块定义 (45)4.2.2 插入带有属性的块参照 (47)实验四用户交互、选择集及扩展属性 (49)一、主要内容 (49)二、学时安排 (49)三、预习内容 (49)四、实验步骤 (49)引用: (49)4.1 用户交互 (49)4.1.1 读入字符串 (50)4.1.2读入Double型数据 (50)4.1.3 读入Point (51)4.1.4 拾取实体 (51)4.1.5 打开dwg文件 (52)4.2 选择集 (54)4.2.1 获得PickFirst选择集 (54)4.2.2 在图形区域中选择对象 (55)4.2.3 添加或合并多个选择集 (56)4.2.4 定义选择集过滤规则 (57)4.2.5 从选择集中移除对象 (58)4.3 扩展属性 (59)实验一 ObjectARX .NET托管开发包的使用一、主要内容1、下载安装ObjectARX .NET托管开发包2、创建AutoCAD命令3、创建自定义用户菜单4、创建自定义用户工具条5、创建用户自定义窗体二、学时安排:2学时三、预习内容及要求回顾ObjectARX API和其托管API之间的对应关系;掌握不同API版本对应的 开发版本;熟悉创建用户需求所要引用命名空间。
开发语言使用C#,AutoCAD版本2012,objectARX2012。
四、下载安装ObjectARX4.1下载ObjectARX不同的ACAD版本用不同的开发包及开发环境⏹ACAD2000~ACAD2002 : VC++ 6.0⏹ACAD2004~ACAD2006 : 2002⏹ACAD2007~ACAD2009 : 2005⏹ACAD2010~ACAD2011 : 2008⏹ACAD2012~ : 20101).在打开打开默认网页浏览器并浏览/forum.php?mod=viewthread&tid=1173&extra=page%3 D1网站.2).或从AutoCAD官网下载4.2 安装ObjectARX向导和托管项目模板1).一旦 ObjectARX SDK被安装,请使用默认目录c:\ObjectARX <Release>浏览它的安装文件夹。
2).浏览安装文件夹后,打开Utils 文件夹和ObjARXWiz文件夹。
3).在 ObjARXWiz 文件夹中, 双击ArxWizards.msi文件。
4).在ObjectARX Wizards for AutoCAD 2010 对话框中, 点击 Next.5).在Enter Your Preferred Default RDS Symbol框中,输入你的公司名字的缩写并点击Next按扭。
你输入的RDS文字被用于默认名字的前缀,这个名字是用ObjectARX 向导与工程模板创建类或命令时使用的。
6).注意RDS代表Registered Developer Symbol, 它用于创建命令和类的标识,以帮助避免与其它应用程序之间的潜在冲突。
7).在Member Variable Wizard页,选择同意"Replace MS Member Variable Wizard by Autodesk Member Variable Wizard"并单击 Next按扭.8).在选择Select Installation Folder(安装文件夹)页,点击向导的Browse按扭指定一个新的安装位置或者同意默认位置。
点击Next按扭.9).再次点击安装向导的Next 按扭。
10).点击向导的 Close按扭退出。
4.3 Hello World程序创建(1)在 2012建项目HelloWorld1.实验采用.NET托管API→从“项目类型”列表中选择第二个“Visual C#”,从模板中选择“控制台应用程序”,输入项目名称“HelloWorld”→单击“确定”按钮。
添加引用,右击引用选择“添加引用”→点击“浏览”→从ObjectARX 安装目录下加载AcCoreMgd.dll、AcCui.dll、Acmgd.dll和AcDbMgd.dll→点击“添加”→点击“确定”。
→使用解决方案资源管理器查看→输入完整程序如下using System;using Autodesk.AutoCAD.Runtime;using Autodesk.AutoCAD.ApplicationServices;using Autodesk.AutoCAD.DatabaseServices;using Autodesk.AutoCAD.Geometry;using Autodesk.AutoCAD.EditorInput;[assembly: CommandClass(typeof(HelloWorld.Hello))]namespace HelloWorld{///<summary>/// Summary description for rdsCommands.///</summary>public class Hello{public Hello(){//// TODO: Add constructor logic here//}// Define Command "AsdkCmd1"[CommandMethod("HW")]static public void HW() // This method can have any name {// Put your command code hereEditor pEd = Application.DocumentManager.MdiActiveDocument.Editor;pEd.WriteMessage("Hello World ");}}}→编译,并在AutoCAD加载DLL→输入命令“HW”,查看运行结果4.4 创建上下文菜单(1)加载命名空间using System;using Autodesk.AutoCAD.Runtime;using Autodesk.AutoCAD.Windows;using Autodesk.AutoCAD.EditorInput; (2)输入完成程序如下using System;using Autodesk.AutoCAD.Windows;using Autodesk.AutoCAD.Runtime;using Autodesk.AutoCAD.EditorInput;[assembly: CommandClass(typeof(MyMenu.MyMenu))]namespace MyMenu{public class MyMenu : IExtensionApplication {ContextMenuExtension MyMenu;//初始化菜单public void Initialize(){AddMenu();}//释放资源,移除菜单public void Terminate(){Remove();}//添加菜单public void AddMenu(){try{MyMenu = new ContextMenuExtension();MyMenu.Title = "An_Menu";Autodesk.AutoCAD.Windows.MenuItem menuItem;menuItem = new Autodesk.AutoCAD.Windows.MenuItem("Welcome");menuItem.Click += new EventHandler(CallbackOnClick);MyMenu.MenuItems.Add(menuItem);Autodesk.AutoCAD.ApplicationServices.Application.AddDefaultContextMenuExtension(MyMenu);}catch (System.Exception ex){}}//移除菜单public void Remove(){try{if (MyMenu != null){Autodesk.AutoCAD.ApplicationServices.Application.RemoveDefaultContextMenuExtension(MyMenu);}}catch (System.Exception ex){}}public void CallbackOnClick(object sender, EventArgs e){} }}(3)编译,并在AutoCAD利用NETLOAD加载dll。
4.5 创建弹出式下拉菜单(1)输入完整程序如下:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;using Autodesk.AutoCAD.Customization;using Autodesk.AutoCAD.ApplicationServices;using Autodesk.AutoCAD.Runtime;namespace TurndownMenu{public class TurndownMenu{CustomizationSection[] partials;CustomizationSection cs;CustomizationSection entCs;int numPartialFiles;bool entCsLoaded;[CommandMethod("Ad_Cui")]public void Ad_Cui_Load(){this.init_Cui();this.add_Menu();this.save_Cui();}private void init_Cui(){string mainCuiFile = (string)Application.GetSystemVariable("MENUNAME");mainCuiFile += ".cuix";cs = new CustomizationSection(mainCuiFile);string entCuiFile = (string)Application.GetSystemVariable("ENTERPRISEMENU");if (entCuiFile.Equals("."))entCsLoaded = false;else{ entCs = new CustomizationSection(entCuiFile);entCsLoaded = true;}partials = new CustomizationSection[cs.PartialCuiFiles.Count];int i = 0;foreach (string fileName in cs.PartialCuiFiles){if (File.Exists(fileName)){partials[i] = new CustomizationSection(fileName); i++;}}numPartialFiles = i;}private void add_Menu(){if (cs.MenuGroup.PopMenus.IsNameFree("测试菜单")){MacroGroup mg = new MacroGroup(cs.MenuGroupName, cs.MenuGroup);MenuMacro m1 = new MenuMacro(mg, "wdgl_cmd1", "^C^CAd_FindCAD","ID_AD_FindCAD");MenuMacro m2 = new MenuMacro(mg, "wdgl_cmd2", "^C^CAd_FileInfo","ID_Ad_FileInfo");MenuMacro m3 = new MenuMacro(mg, "wdgl_cmd3", "^C^CAd_Help","ID_Ad_Help");System.Collections.Specialized.StringCollection pmAliases = new System.Collections.Specialized.StringCollection();pmAliases.Add("POP12");PopMenu pm = new PopMenu("测试菜单", pmAliases, "测试菜单",cs.MenuGroup);PopMenuItem pmi = new PopMenuItem(m1, "测试菜单", pm, -1);pmi = new PopMenuItem(m2, "测试菜单", pm, -1);pmi = new PopMenuItem(pm, -1);pmi = new PopMenuItem(m3, "菜单项", pm, -1);addMenu2Workspaces(pm);foreach (Workspace wk in cs.Workspaces){WorkspacePopMenu wkpm = new WorkspacePopMenu(wk, pm);wkpm.Display = 1;}}else { }}private void addMenu2Workspaces(PopMenu pm){}private void save_Cui(){if (cs.IsModified)cs.Save();for (int i = 0; i < numPartialFiles; i++){if (partials[i].IsModified)partials[i].Save();}if (entCsLoaded && entCs.IsModified)entCs.Save();string flName = cs.CUIFileBaseName;Application.SetSystemVariable("FILEDIA", 0);Application.DocumentManager.MdiActiveDocument.SendStringToExecute("cuiunload " + flName + " ", false, false, false);Application.DocumentManager.MdiActiveDocument.SendStringToExecute("cuiload " + flName + " filedia 1 ", false, false, false);}}}(2)编译,并在AutoCAD利用NETLOAD加载dll。