生成三维模型

在autocad使用代码生成三维模型

[CommandMethod("cww")]
        public void CreateWedge()
        {
            // Get the current document and database, and start a transaction
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            Editor ed = acDoc.Editor;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // 以只读方式打开块表记录   Open the Block table record for read
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                             OpenMode.ForRead) as BlockTable;

                // 以写方式打开模型空间块表记录   Open the Block table record Model space for write
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                                OpenMode.ForWrite) as BlockTableRecord;
                //插入点
                PromptPointOptions point = new PromptPointOptions("\n请选择插入点");
                PromptPointResult originPoint;
                originPoint = ed.GetPoint(point);
                Point3d origin = originPoint.Value;

                // Create a 3D solid wedge
                Solid3d acSol3D = new Solid3d();
                acSol3D.RecordHistory = true;
                //acSol3D.SetDatabaseDefaults();
                //acSol3D.CreateWedge(10, 15, 20);
                //acSol3D.CreateSphere(12.0);
                //acSol3D.CreateBox(100, 100, 100);
                acSol3D.CreateSphere(50);
                //acSol3D.CreateExtensionDictionary();
                // Position the center of the 3D solid at (5,5,0)
                acSol3D.TransformBy(Matrix3d.Displacement(origin -
                                                          Point3d.Origin));

                // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
                acBlkTblRec.AppendEntity(acSol3D);
                acTrans.AddNewlyCreatedDBObject(acSol3D, true);

                Solid3d acSol3D2 = new Solid3d();
                acSol3D2.RecordHistory = true;
                //acSol3D2.SetDatabaseDefaults();
                //acSol3D.CreateWedge(10, 15, 20);
                //acSol3D.CreateSphere(12.0);
                acSol3D2.CreateBox(500, 500, 100);
                // Position the center of the 3D solid at (5,5,0)
                acSol3D2.TransformBy(Matrix3d.Displacement(origin -
                                                          Point3d.Origin));

                // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
                acBlkTblRec.AppendEntity(acSol3D2);
                acTrans.AddNewlyCreatedDBObject(acSol3D2, true);

                Solid3d acSol3D3 = new Solid3d();
                acSol3D3.SetDatabaseDefaults();
                //acSol3D.CreateWedge(10, 15, 20);
                //acSol3D.CreateSphere(12.0);
                acSol3D3.CreateBox(500, 500, 100);
                // Position the center of the 3D solid at (5,5,0)
                acSol3D3.TransformBy(Matrix3d.Displacement(new Point3d(origin.X+500,origin.Y+500,origin.Z+500) -
                                                          Point3d.Origin));

                // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
                acBlkTblRec.AppendEntity(acSol3D3);
                acTrans.AddNewlyCreatedDBObject(acSol3D3, true);

                Solid3d acSol3D4 = new Solid3d();
                acSol3D4.SetDatabaseDefaults();
                //acSol3D.CreateWedge(10, 15, 20);
                //acSol3D.CreateSphere(12.0);
                acSol3D4.CreateBox(500, 500, 100);
                // Position the center of the 3D solid at (5,5,0)
                acSol3D4.TransformBy(Matrix3d.Displacement(new Point3d(origin.X + 500, origin.Y + 500, origin.Z + 100) -
                                                          Point3d.Origin));

                // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
                acBlkTblRec.AppendEntity(acSol3D4);
                acTrans.AddNewlyCreatedDBObject(acSol3D4, true);

                Solid3d acSol3D5 = new Solid3d();
                acSol3D5.SetDatabaseDefaults();
                //acSol3D.CreateWedge(10, 15, 20);
                //acSol3D.CreateSphere(12.0);
                acSol3D5.CreateBox(100, 500, 900);
                acSol3D5.Color = Autodesk.AutoCAD.Colors.Color.FromColor(System.Drawing.Color.DarkBlue);
                // Position the center of the 3D solid at (5,5,0)
                acSol3D5.TransformBy(Matrix3d.Displacement(new Point3d(origin.X + 300, origin.Y, origin.Z+400) -
                                                          Point3d.Origin));

                // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
                acBlkTblRec.AppendEntity(acSol3D5);
                acTrans.AddNewlyCreatedDBObject(acSol3D5, true);

                acTrans.Commit();
            }
        }

posted @ 2012-12-19 15:06  HongEgg  阅读(215)  评论(0编辑  收藏  举报