Пример #1
0
        public SubDMeshHandler.MeshGenerationResult GenerateSubDMesh()
        {
            if (this.list_4 == null)
            {
                throw new InvalidOperationException("Can not start SubDMesh handler: no triangles defined.");
            }
            if (this.list_4.Count < 1)
            {
                return(new SubDMeshHandler.MeshGenerationResult());
            }
            Database workingDatabase = HostApplicationServices.WorkingDatabase;
            Editor   arg_3C_0        = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

            SubDMeshHandler.MeshGenerationResult result;
            using (Transaction transaction = workingDatabase.TransactionManager.StartTransaction())
            {
                this.BuildDataStructure(true);
                Point3dCollection point3dCollection = new Point3dCollection();
                for (int i = 0; i < this.list_0.Count; i++)
                {
                    point3dCollection.Add(new Point3d(this.list_0[i].X, this.list_0[i].Y, this.list_0[i].Z));
                }
                Int32Collection int32Collection = new Int32Collection(3 * this.list_4.Count);
                for (int j = 0; j < this.list_4.Count; j++)
                {
                    int32Collection.Add(3);
                    int32Collection.Add(this.list_1[j]);
                    int32Collection.Add(this.list_2[j]);
                    int32Collection.Add(this.list_3[j]);
                }
                SubDMesh subDMesh = new SubDMesh();
                subDMesh.SetDatabaseDefaults();
                subDMesh.SetSubDMesh(point3dCollection, int32Collection, 0);
                subDMesh.SetPropertiesFrom(this.list_4[0].AcDbFace);
                BlockTable       blockTable       = (BlockTable)transaction.GetObject(workingDatabase.BlockTableId, (OpenMode)1);
                BlockTableRecord blockTableRecord = (BlockTableRecord)transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], (OpenMode)1);
                blockTableRecord.AppendEntity(subDMesh);
                transaction.AddNewlyCreatedDBObject(subDMesh, true);
                transaction.Commit();
                result = new SubDMeshHandler.MeshGenerationResult
                {
                    objectId_0 = subDMesh.ObjectId,
                    int_0      = this.list_0.Count,
                    int_1      = this.list_4.Count,
                    string_0   = DBManager.GetLayerName(subDMesh.LayerId)
                };
            }
            return(result);
        }
Пример #2
0
        /// <summary> 将任意实体转换为网格 </summary>
        public static void CreateMeshFromSolid()
        {
            //Select a solid.
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptEntityOptions opts = new PromptEntityOptions("\r\n" + "Select Solid:");

            opts.SetRejectMessage("\r\n" + "That\'s not a solid!");
            opts.AddAllowedClass(typeof(Solid3d), false);
            PromptEntityResult res = ed.GetEntity(opts);

            //Exit sub if user cancelled selection.
            if (res.Status != PromptStatus.OK)
            {
                return;
            }

            //Usual transaction stuff
            Database db = Application.DocumentManager.MdiActiveDocument.Database;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                Solid3d   mySolid = tr.GetObject(res.ObjectId, OpenMode.ForRead, false) as Solid3d;
                Extents3d ext     = mySolid.Bounds.Value;
                Vector3d  vec     = ext.MaxPoint - ext.MinPoint;

                // 实体转换为网格的生成算法,即平滑或插值的参数
                //Define params governing mesh generation algorithm(See ObjectARX helpfiles for explanation of params you may need to change them depending on the scale of the solid)
                MeshFaceterData myFaceterData = new MeshFaceterData(0.01 * vec.Length, 40 * Math.PI / 180, 2, 2, 15, 5, 5, 0);

                //Create new mesh from solid (smoothing level 1)
                MeshDataCollection meshData = SubDMesh.GetObjectMesh(mySolid, myFaceterData);
                SubDMesh           myMesh   = new SubDMesh();
                myMesh.SetSubDMesh(meshData.VertexArray, meshData.FaceArray, 1);

                //Add mesh to database. (Don't remove solid).
                myMesh.SetDatabaseDefaults();
                var btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                btr.AppendEntity(myMesh);
                tr.AddNewlyCreatedDBObject(myMesh, true);

                //Our work here is done
                tr.Commit();
            }
        }
Пример #3
0
        static public void MeshFromSolid()
        {
            Utils.Utils.Init();

            Document acDoc   = acApp.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            Editor   ed      = acDoc.Editor;

            while (true)
            {
                // Ask the user to select a solid
                PromptEntityOptions peo = new PromptEntityOptions("Select a 3D solid");
                peo.SetRejectMessage("\nA 3D solid must be selected.");
                peo.AddAllowedClass(typeof(Solid3d), true);
                PromptEntityResult per = ed.GetEntity(peo);

                if (per.Status == PromptStatus.Cancel || per.Status == PromptStatus.Error)
                {
                    Utils.Utils.End(); return;
                }

                using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                {
                    // Open the Block table 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;

                    Solid3d sol = acTrans.GetObject(per.ObjectId, OpenMode.ForWrite) as Solid3d;

                    try
                    {
                        // Set mesh/faces properties
                        MeshFaceterData mfd = new MeshFaceterData();
                        mfd.FaceterMeshType      = 2;
                        mfd.FaceterDevNormal     = 40;
                        mfd.FaceterDevSurface    = 0.05;
                        mfd.FaceterGridRatio     = 0;
                        mfd.FaceterMaxEdgeLength = 0;

                        MeshDataCollection md = SubDMesh.GetObjectMesh(sol, mfd);

                        // Create mesh
                        SubDMesh mesh = new SubDMesh();
                        mesh.SetDatabaseDefaults();
                        mesh.SetSubDMesh(md.VertexArray, md.FaceArray, 0);
                        mesh.Layer = sol.Layer;

                        // Add mesh to DB
                        acBlkTblRec.AppendEntity(mesh);
                        acTrans.AddNewlyCreatedDBObject(mesh, true);

                        // Delete solid object
                        sol.Erase(true);

                        acTrans.Commit();
                    }
                    catch (System.Exception ex)
                    {
                        ed.WriteMessage("Exception: {0}", ex.Message);
                    }
                }
            }
        }
Пример #4
0
        public void ImportUit3ds()
        {
            // Verkrijg document en database
            Document acDoc   = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;

            acCurDb.Surfu = 0;
            acCurDb.Surfv = 0;

            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // Open de block-tabel
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;

                // Open de Model space om in te schrijven
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                //Bestand openen om binair te lezen
                binReader = new BinaryReader(File.Open(@"C:/3DGISBuffer.dat", FileMode.Open));

                //Loop door objecten
                int aantalobjecten = binReader.ReadInt32();
                for (int i = 0; i < aantalobjecten; i++)
                {
                    Point3dCollection vertarray = new Point3dCollection();
                    Int32Collection   facearray = new Int32Collection();

                    // Maak een subdivision-mesh aan
                    SubDMesh sdm = new SubDMesh();
                    sdm.SetDatabaseDefaults();

                    // Voeg het object toe aan de block-tabel
                    acBlkTblRec.AppendEntity(sdm);
                    acTrans.AddNewlyCreatedDBObject(sdm, true);

                    //objectkleur lezen
                    byte kleur_r = binReader.ReadByte();
                    byte kleur_g = binReader.ReadByte();
                    byte kleur_b = binReader.ReadByte();

                    //solidvlag lezen
                    Boolean solidvlag = LeesBoolean();

                    // Vertexarray vullen met vertices
                    Point3dCollection acPts3dPFMesh = new Point3dCollection();
                    Int32             nvts          = binReader.ReadInt32();

                    for (int j = 1; j <= nvts; j++)
                    {
                        Single nX = binReader.ReadSingle();
                        Single nY = binReader.ReadSingle();
                        Single nZ = binReader.ReadSingle();
                        vertarray.Add(new Point3d(nX, nY, nZ));
                    }

                    //Facearray vullen met faces
                    int nfcs = binReader.ReadInt32();
                    for (int j = 1; j <= nfcs; j++)
                    {
                        int fc1 = binReader.ReadInt32();
                        int fc2 = binReader.ReadInt32();
                        int fc3 = binReader.ReadInt32();

                        facearray.Add(3);
                        facearray.Add(fc1 - 1);
                        facearray.Add(fc2 - 1);
                        facearray.Add(fc3 - 1);
                    }

                    //Vertex- en facearray toevoegen aan mesh, smoothlevel 0
                    sdm.SetSubDMesh(vertarray, facearray, 0);

                    Entity pMijnObj = null;
                    if (solidvlag)
                    {
                        Autodesk.AutoCAD.DatabaseServices.Solid3d pSolid = sdm.ConvertToSolid(false, false);
                        pMijnObj = (Entity)pSolid;
                    }
                    else
                    {
                        Autodesk.AutoCAD.DatabaseServices.Surface pSurface = sdm.ConvertToSurface(false, false);
                        pMijnObj = (Entity)pSurface;
                    }
                    acBlkTblRec.AppendEntity(pMijnObj);
                    acTrans.AddNewlyCreatedDBObject(pMijnObj, true);

                    //Verwijder mesh
                    sdm.Erase();


                    // Schrijf objectnaam naar Xrecord in de entity-dictionary van de surface
                    SaveXRecord(pMijnObj, LeesAttributen(), "GISData", acTrans);

                    //kleur van het object updaten
                    pMijnObj.Color = Color.FromRgb(kleur_r, kleur_g, kleur_b);
                }//einde for-loop

                // Schrijf het object naar de AutoCAD-database en sluit binReader
                acTrans.Commit();
                binReader.Close();
            } //einde using transaction
        }     //einde method