ahlzl 发表于 2009-5-20 07:40:00

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

AutoCAD 2010的那两个托管DLL文件中,增加了对“放样”和“扫琼”的支持,使得我们用C#进行三维建模的手段更加丰富了!
以前的CAD版本,用C#能进行“放样曲面”和“扫琼曲面”的操作,但不能进行实体的操作。

// 放样示例:天圆地方.

public void Test()
{
    Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
    Polyline ent1 = new Polyline();
    ent1.AddVertexAt(0, new Point2d(50, 50), 0, 0, 0);
    ent1.AddVertexAt(1, new Point2d(-50, 50), 0, 0, 0);
    ent1.AddVertexAt(2, new Point2d(-50, -50), 0, 0, 0);
    ent1.AddVertexAt(3, new Point2d(50, -50), 0, 0, 0);
    ent1.Closed = true;
    Circle ent2 = new Circle(new Point3d(0, 0, 200), new Vector3d(0, 0, 1), 30);
    Entity[] crossEnts = new Entity;
    crossEnts.SetValue(ent1, 0);
    crossEnts.SetValue(ent2, 1);
    Entity[] guideCurs = new Entity;
    LoftOptions loftOpt = new LoftOptions();
    Solid3d solid3dEnt = new Solid3d();
    solid3dEnt.RecordHistory = true;
    solid3dEnt.CreateLoftedSolid(crossEnts, guideCurs, null, loftOpt);
    AppendEntity(solid3dEnt);
}
ObjectId AppendEntity(Entity ent)
{
    Database db = HostApplicationServices.WorkingDatabase;
    ObjectId entId;
    using (Transaction trans = db.TransactionManager.StartTransaction())
    {
      BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,
            OpenMode.ForRead);
      BlockTableRecord btr = (BlockTableRecord)trans.GetObject
            (bt, OpenMode.ForWrite);
      entId = btr.AppendEntity(ent);
      trans.AddNewlyCreatedDBObject(ent, true);
      trans.Commit();
    }
    return entId;
}

雪山飞狐_lzh 发表于 2009-5-20 09:38:00

<p>对3d的东西一向不太熟悉,相信各位朋友也一样,</p><p>但在AutoCad不断加强3d功能的现在,确实应该引起重视</p><p>也希望ahlzl斑竹多发一些相关的东西,:)</p>

gyj666 发表于 2011-2-12 20:46:32

紧急求助,需要一个VBA的例子,请ahlzl 斑竹不吝赐教呀!!!

lamntree 发表于 2015-9-12 20:52:15

天圆地方曲面

本帖最后由 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();
            }
      }
页: [1]
查看完整版本: AutoCAD 2010的C#实体放样示例——天圆地方