Exemplo n.º 1
0
        private static void CreateDrawingErrorReferences(Point3d startPoint, Point3d endPoint,
                                                         global::Autodesk.AutoCAD.DatabaseServices.Database db,
                                                         CoordinateSystem3d ucs)
        {
            try
            {
                var tr = db.TransactionManager.StartTransaction();

                using (tr)
                {
                    var btr =
                        (BlockTableRecord)tr.GetObject(
                            db.CurrentSpaceId,
                            OpenMode.ForWrite
                            );

                    var cir = new Circle(startPoint, ucs.Zaxis, 5);
                    cir.Color = Color.FromColorIndex(ColorMethod.ByColor, 1);
                    btr.AppendEntity(cir);

                    tr.AddNewlyCreatedDBObject(cir, true);
                    tr.Commit();
                }
            }
            catch
            {
                MessengerManager.MessengerManager.AddLog(
                    "\nError calculating enclosing circle."
                    );
            }
        }
Exemplo n.º 2
0
        //[CommandMethod("PGA-Hatch")]
        static public void TestHatch(Polyline poly)
        {
            if (poly == null)
            {
                throw new ArgumentNullException(nameof(poly));
            }

            Document doc = Active.Document;

            global::Autodesk.AutoCAD.DatabaseServices.Database db = doc.Database;
            Editor ed = doc.Editor;

            try
            {
                using (Transaction Tx = db.TransactionManager.StartTransaction())
                {
                    ObjectId ModelSpaceId =
                        SymbolUtilityServices.GetBlockModelSpaceId(db);

                    BlockTableRecord btr = Tx.GetObject(ModelSpaceId,
                                                        OpenMode.ForWrite) as BlockTableRecord;



                    ObjectIdCollection ObjIds = new ObjectIdCollection();
                    ObjIds.Add(poly.Id);

                    Hatch    oHatch = new Hatch();
                    Vector3d normal = new Vector3d(0.0, 0.0, 1.0);
                    oHatch.Normal       = normal;
                    oHatch.Elevation    = 0.0;
                    oHatch.PatternScale = 2.0;
                    oHatch.SetHatchPattern(HatchPatternType.PreDefined, "ZIGZAG");
                    oHatch.ColorIndex = poly.Color.ColorIndex;
                    oHatch.Layer      = poly.Layer;

                    btr.AppendEntity(oHatch);
                    Tx.AddNewlyCreatedDBObject(oHatch, true);
                    //this works ok
                    oHatch.Associative = true;
                    oHatch.AppendLoop((int)HatchLoopTypes.Default, ObjIds);
                    oHatch.EvaluateHatch(true);

                    Tx.Commit();
                }
            }
            catch (System.Exception ex)
            {
                // throw;
            }
        }
Exemplo n.º 3
0
            static public void RegionToPolyline(ObjectId regId)
            {
                Document doc = Active.Document;

                global::Autodesk.AutoCAD.DatabaseServices.Database db = doc.Database;
                Editor ed = doc.Editor;

                Transaction tr =
                    doc.TransactionManager.StartTransaction();

                using (tr)
                {
                    BlockTable bt =
                        (BlockTable)tr.GetObject(
                            db.BlockTableId,
                            OpenMode.ForRead);
                    BlockTableRecord btr =
                        (BlockTableRecord)tr.GetObject(
                            bt[BlockTableRecord.ModelSpace],
                            OpenMode.ForRead);

                    Region reg =
                        tr.GetObject(
                            regId,
                            OpenMode.ForRead) as Region;


                    if (reg != null)
                    {
                        DBObjectCollection objs =
                            PolylineFromRegion(reg);

                        // Append our new entities to the database

                        btr.UpgradeOpen();

                        foreach (Entity ent in objs)
                        {
                            btr.AppendEntity(ent);
                            tr.AddNewlyCreatedDBObject(ent, true);
                        }

                        // Finally we erase the original region

                        reg.UpgradeOpen();
                        reg.Erase();
                    }
                    tr.Commit();
                }
            }
Exemplo n.º 4
0
 private static bool IsLayerDefined(global::Autodesk.AutoCAD.DatabaseServices.Database db, string layername)
 {
     try
     {
         if (LayerManager.IsDefined(db, layername))
         {
             return(LayerManager.IsDefined(db, layername));
         }
         throw new Exception("Layer not found: " + layername);
     }
     catch (Exception ex)
     {
         MessengerManager.MessengerManager.AddLog(ex.Message);
     }
     return(false);
 }
Exemplo n.º 5
0
        private static void CombineBlocksIntoLibrary()

        {
            var doc =
                Application.DocumentManager.MdiActiveDocument;

            var ed = doc.Editor;

            var destDb = doc.Database;


            //Get name of folder from which to load and import blocks


            var pr =
                ed.GetString("\nEnter the folder of source drawings: ");


            if (pr.Status != PromptStatus.OK)
            {
                return;
            }

            var pathName = pr.StringResult;


            // Check the folder exists


            if (!Directory.Exists(pathName))

            {
                ed.WriteMessage(
                    "\nDirectory does not exist: {0}", pathName
                    );

                return;
            }


            // Get the names of our DWG files in that folder


            var fileNames = Directory.GetFiles(pathName, "*.dwg");


            // A counter for the files we've imported


            int imported = 0, failed = 0;


            // For each file in our list


            foreach (var fileName in fileNames)

            {
                // Double-check we have a DWG file (probably unnecessary)


                if (fileName.EndsWith(
                        ".dwg",
                        StringComparison.InvariantCultureIgnoreCase
                        )
                    )

                {
                    // Catch exceptions at the file level to allow skipping


                    try

                    {
                        // Suggestion from Thorsten Meinecke...


                        var destName =
                            SymbolUtilityServices.GetSymbolNameFromPathName(
                                fileName, "dwg"
                                );


                        // And from Dan Glassman...


                        destName =
                            SymbolUtilityServices.RepairSymbolName(
                                destName, false
                                );


                        // Create a source database to load the DWG into


                        using (var db = new global::Autodesk.AutoCAD.DatabaseServices.Database(false, true))

                        {
                            // Read the DWG into our side database


                            db.ReadDwgFile(fileName, FileShare.Read, true, "");

                            var isAnno = db.AnnotativeDwg;


                            // Insert it into the destination database as

                            // a named block definition


                            var btrId = destDb.Insert(
                                destName,
                                db,
                                false
                                );


                            if (isAnno)

                            {
                                // If an annotative block, open the resultant BTR

                                // and set its annotative definition status


                                var tr =
                                    destDb.TransactionManager.StartTransaction();

                                using (tr)

                                {
                                    var btr =
                                        (BlockTableRecord)tr.GetObject(
                                            btrId,
                                            OpenMode.ForWrite
                                            );

                                    btr.Annotative = AnnotativeStates.True;

                                    tr.Commit();
                                }
                            }


                            // Print message and increment imported block counter


                            ed.WriteMessage("\nImported from \"{0}\".", fileName);

                            imported++;
                        }
                    }

                    catch (Exception ex)

                    {
                        ed.WriteMessage(
                            "\nProblem importing \"{0}\": {1} - file skipped.",
                            fileName, ex.Message
                            );

                        failed++;
                    }
                }
            }


            ed.WriteMessage(
                "\nImported block definitions from {0} files{1} in " +
                "\"{2}\" into the current drawing.",
                imported,
                failed > 0 ? " (" + failed + " failed)" : "",
                pathName
                );
        }
Exemplo n.º 6
0
            static public void RegionToPolyline(ObjectId regId)
            {
                Document doc =
                    Application.DocumentManager.MdiActiveDocument;

                global::Autodesk.AutoCAD.DatabaseServices.Database db = doc.Database;
                Editor ed = doc.Editor;

                //PromptEntityOptions peo =
                //  new PromptEntityOptions("\nSelect a region:");
                //peo.SetRejectMessage("\nMust be a region.");
                //peo.AddAllowedClass(typeof(Region), true);

                //PromptEntityResult per =
                //  ed.GetEntity(peo);

                //if (per.Status != PromptStatus.OK)
                //    return;

                Transaction tr =
                    doc.TransactionManager.StartTransaction();

                using (tr)
                {
                    BlockTable bt =
                        (BlockTable)tr.GetObject(
                            db.BlockTableId,
                            OpenMode.ForRead);
                    BlockTableRecord btr =
                        (BlockTableRecord)tr.GetObject(
                            bt[BlockTableRecord.ModelSpace],
                            OpenMode.ForRead);

                    Region reg =
                        tr.GetObject(
                            regId,
                            OpenMode.ForRead) as Region;


                    if (reg != null)
                    {
                        DBObjectCollection objs =
                            PolylineFromRegion(reg);

                        // Append our new entities to the database

                        btr.UpgradeOpen();

                        foreach (Entity ent in objs)
                        {
                            btr.AppendEntity(ent);
                            tr.AddNewlyCreatedDBObject(ent, true);
                        }

                        // Finally we erase the original region

                        reg.UpgradeOpen();
                        reg.Erase();
                    }
                    tr.Commit();
                }
            }