明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 5122|回复: 3

[三维] AutoCAD 2010的C#实体放样示例——天圆地方

[复制链接]
发表于 2009-5-20 07:40 | 显示全部楼层 |阅读模式
AutoCAD 2010的那两个托管DLL文件中,增加了对“放样”和“扫琼”的支持,使得我们用C#进行三维建模的手段更加丰富了!
以前的CAD版本,用C#能进行“放样曲面”和“扫琼曲面”的操作,但不能进行实体的操作。
  1. // 放样示例:天圆地方.
  2. [CommandMethod("Test")]
  3. public void Test()
  4. {
  5.     Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  6.     Polyline ent1 = new Polyline();
  7.     ent1.AddVertexAt(0, new Point2d(50, 50), 0, 0, 0);
  8.     ent1.AddVertexAt(1, new Point2d(-50, 50), 0, 0, 0);
  9.     ent1.AddVertexAt(2, new Point2d(-50, -50), 0, 0, 0);
  10.     ent1.AddVertexAt(3, new Point2d(50, -50), 0, 0, 0);
  11.     ent1.Closed = true;
  12.     Circle ent2 = new Circle(new Point3d(0, 0, 200), new Vector3d(0, 0, 1), 30);
  13.     Entity[] crossEnts = new Entity[2];
  14.     crossEnts.SetValue(ent1, 0);
  15.     crossEnts.SetValue(ent2, 1);
  16.     Entity[] guideCurs = new Entity[0];
  17.     LoftOptions loftOpt = new LoftOptions();
  18.     Solid3d solid3dEnt = new Solid3d();
  19.     solid3dEnt.RecordHistory = true;
  20.     solid3dEnt.CreateLoftedSolid(crossEnts, guideCurs, null, loftOpt);
  21.     AppendEntity(solid3dEnt);
  22. }
  23. ObjectId AppendEntity(Entity ent)
  24. {
  25.     Database db = HostApplicationServices.WorkingDatabase;
  26.     ObjectId entId;
  27.     using (Transaction trans = db.TransactionManager.StartTransaction())
  28.     {
  29.         BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,
  30.             OpenMode.ForRead);
  31.         BlockTableRecord btr = (BlockTableRecord)trans.GetObject
  32.             (bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  33.         entId = btr.AppendEntity(ent);
  34.         trans.AddNewlyCreatedDBObject(ent, true);
  35.         trans.Commit();
  36.     }
  37.     return entId;
  38. }

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x

评分

参与人数 1威望 +1 明经币 +5 金钱 +20 贡献 +5 激情 +5 收起 理由
雪山飞狐_lzh + 1 + 5 + 20 + 5 + 5 【精华】好程序

查看全部评分

发表于 2009-5-20 09:38 | 显示全部楼层

对3d的东西一向不太熟悉,相信各位朋友也一样,

但在AutoCad不断加强3d功能的现在,确实应该引起重视

也希望ahlzl斑竹多发一些相关的东西,:)

发表于 2011-2-12 20:46 | 显示全部楼层
紧急求助,需要一个VBA的例子,请ahlzl 斑竹不吝赐教呀!!!
发表于 2015-9-12 20:52 | 显示全部楼层

天圆地方曲面

本帖最后由 lamntree 于 2015-9-12 20:53 编辑

直接loft的天圆地方实际做钣金是做不出来的,我把它分成4个三角面,4个曲面,组合出来,这样就和实际钣金做出来的一样了
public void hopper3d()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                PromptPointResult ppr = ed.GetPoint("\nspecify inset point");
                if (ppr.Status == PromptStatus.OK)
                {
                    Matrix3d UCS = ed.CurrentUserCoordinateSystem;
                    Point3d pt = ppr.Value.TransformBy(UCS);
                    PromptDoubleResult pdrDia = ed.GetDouble("\nspecify diameter of circle");
                    if (pdrDia.Status == PromptStatus.OK)
                    {                        
                        double D = pdrDia.Value;
                        PromptDoubleResult pdrL = ed.GetDouble("\nspecify Length");
                        if (pdrL.Status == PromptStatus.OK)
                        {
                            double L = pdrL.Value;
                            PromptDoubleResult pdrW = ed.GetDouble("\nspecify width");
                            if (pdrW.Status == PromptStatus.OK)
                            {
                                double W = pdrW.Value;
                                PromptDoubleResult pdrH = ed.GetDouble("\nspecify height");
                                if (pdrH.Status == PromptStatus.OK)
                                {
                                    Application.SetSystemVariable("clayer", "0");
                                    Vector3d vec = pt - new Point3d(0, 0, 0);
                                    double H = pdrH.Value;
                                    LoftOptions lo = new LoftOptions();
                                    Line l1 = new Line(new Point3d(0, -D / 2, 0), new Point3d(-L / 2, -W / 2, -H));                                    
                                    l1.TransformBy(Matrix3d.Displacement(vec));
                                    Line l2 = new Line(new Point3d(0, -D / 2, 0), new Point3d(L / 2, -W / 2, -H));                                    
                                    l2.TransformBy(Matrix3d.Displacement(vec));
                                    Entity[] l1l2=new Entity[]{l1,l2};
                                    LoftedSurface lf1 = new LoftedSurface();
                                    lf1.CreateLoftedSurface(l1l2, null, null, lo);
                                    btr.AppendEntity(lf1);
                                    trans.AddNewlyCreatedDBObject(lf1, true);                                   

                                    Arc a1 = new Arc(new Point3d(0, 0, 0), D / 2, -Math .PI /2, 0);                                   
                                    a1.TransformBy(Matrix3d.Displacement(vec));
                                    Entity[] l2a1 = new Entity[] { l2, a1 };
                                    LoftedSurface lf2 = new LoftedSurface();
                                    lf2.CreateLoftedSurface(l2a1, null, null, lo);
                                    btr.AppendEntity(lf2);
                                    trans.AddNewlyCreatedDBObject(lf2, true);                                    

                                    Line l3 = new Line(new Point3d(D / 2, 0, 0), new Point3d(L / 2, -W / 2, -H));                                    
                                    l3.TransformBy(Matrix3d.Displacement(vec));
                                    Line l4 = new Line(new Point3d(D / 2, 0, 0), new Point3d(L / 2, W / 2, -H));                                   
                                    l4.TransformBy(Matrix3d.Displacement(vec));
                                    Entity[] l3l4 = new Entity[] { l3, l4 };
                                    LoftedSurface lf3 = new LoftedSurface();
                                    lf3.CreateLoftedSurface(l3l4, null, null, lo);
                                    btr.AppendEntity(lf3);
                                    trans.AddNewlyCreatedDBObject(lf3, true);                                   

                                    Arc a2 = new Arc(new Point3d(0, 0, 0), D / 2,  0,Math.PI / 2);                                    
                                    a2.TransformBy(Matrix3d.Displacement(vec));
                                    Entity[] l4a2 = new Entity[] { l4, a2 };
                                    LoftedSurface lf4 = new LoftedSurface();
                                    lf4.CreateLoftedSurface(l4a2, null, null, lo);
                                    btr.AppendEntity(lf4);
                                    trans.AddNewlyCreatedDBObject(lf4, true);                                   

                                    Line l5 = new Line(new Point3d(0, D / 2, 0), new Point3d(L / 2, W / 2, -H));                                   
                                    l5.TransformBy(Matrix3d.Displacement(vec));
                                    Line l6 = new Line(new Point3d(0, D / 2, 0), new Point3d(-L / 2, W / 2, -H));                                    
                                    l6.TransformBy(Matrix3d.Displacement(vec));
                                    Entity[] l5l6 = new Entity[] { l5, l6 };
                                    LoftedSurface lf5 = new LoftedSurface();
                                    lf5.CreateLoftedSurface(l5l6, null, null, lo);
                                    btr.AppendEntity(lf5);
                                    trans.AddNewlyCreatedDBObject(lf5, true);                                   

                                    Arc a3 = new Arc(new Point3d(0, 0, 0), D / 2, Math.PI / 2,Math.PI );                                    
                                    a3.TransformBy(Matrix3d.Displacement(vec));
                                    Entity[] l6a3 = new Entity[] { l6, a3 };
                                    LoftedSurface lf6 = new LoftedSurface();
                                    lf6.CreateLoftedSurface(l6a3, null, null, lo);
                                    btr.AppendEntity(lf6);
                                    trans.AddNewlyCreatedDBObject(lf6, true);                                 

                                    Line l7 = new Line(new Point3d(-D / 2,0, 0), new Point3d(-L / 2, W / 2, -H));                                    
                                    l7.TransformBy(Matrix3d.Displacement(vec));
                                    Line l8 = new Line(new Point3d(-D / 2, 0, 0), new Point3d(-L / 2, -W / 2, -H));                                    
                                    l8.TransformBy(Matrix3d.Displacement(vec));
                                    Entity[] l7l8 = new Entity[] { l7, l8 };
                                    LoftedSurface lf7 = new LoftedSurface();
                                    lf7.CreateLoftedSurface(l7l8, null, null, lo);
                                    btr.AppendEntity(lf7);
                                    trans.AddNewlyCreatedDBObject(lf7, true);                                   

                                    Arc a4 = new Arc(new Point3d(0, 0, 0), D / 2, Math.PI, Math.PI*3/2);                                 
                                    a4.TransformBy(Matrix3d.Displacement(vec));
                                    Entity[] l8a4 = new Entity[] { l8, a4 };
                                    LoftedSurface lf8 = new LoftedSurface();
                                    lf8.CreateLoftedSurface(l8a4, null, null, lo);
                                    btr.AppendEntity(lf8);
                                    trans.AddNewlyCreatedDBObject(lf8, true);                                    
                                }
                            }
                        }
                    }
                }
                trans.Commit();
            }
        }

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-25 14:08 , Processed in 0.205137 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表