Пример #1
0
        // Group objects
        // <param name="idCol"></param>
        // <param name="groupName"></param>
        // <returns>id of group object</returns>
        public static ObjectId createGroup(ObjectIdCollection idCol, String groupName)
        {
            ObjectId    groupId;
            Document    doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database    db  = doc.Database;
            Editor      ed  = doc.Editor;
            Transaction tr  = db.TransactionManager.StartTransaction();

            using (tr)
            {
                DBDictionary gd = (DBDictionary)tr.GetObject(db.GroupDictionaryId,
                                                             OpenMode.ForRead);
                Group group = new Group("my group", true);
                gd.UpgradeOpen();
                ObjectId grpId = gd.SetAt(groupName, group);
                tr.AddNewlyCreatedDBObject(group, true);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                btr.AssumeOwnershipOf(idCol);
                group.InsertAt(0, idCol);
                tr.Commit();
                tr.Dispose();
                groupId = grpId;
            }
            return(groupId);
        }
Пример #2
0
        public static void Finalise()
        {
            Document acDoc   = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;

            using (Transaction tr = acCurDb.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(acCurDb.BlockTableId, OpenMode.ForRead);

                ObjectId         newBlockId = bt[PlotType.CurrentOpen.PlotTypeName];
                BlockTableRecord btr        = tr.GetObject(newBlockId, OpenMode.ForWrite) as BlockTableRecord;
                foreach (ObjectId e in btr)
                {
                    Entity temp = tr.GetObject(e, OpenMode.ForWrite) as Entity;
                    temp.Erase();
                }

                ObjectIdCollection plotObjects = new ObjectIdCollection();

                plotObjects.Add(PlotType.CurrentOpen.BackgroundBlockID);
                plotObjects.Add(PlotType.CurrentOpen.BasepointID);

                foreach (WallSegment ws in PlotType.CurrentOpen.Segments)
                {
                    plotObjects.Add(ws.PerimeterLine);
                }

                foreach (ObjectId c in PlotType.CurrentOpen.AccessPointLocations.Collection)
                {
                    plotObjects.Add(c);
                }

                btr.AssumeOwnershipOf(plotObjects);

                tr.Commit();
            }

            //Triggeer regen to update blocks display
            //alternatively http://adndevblog.typepad.com/autocad/2012/05/redefining-a-block.html
            Application.DocumentManager.CurrentDocument.Editor.Regen();

            //Add to the document store
            CivilDocumentStore cds = acDoc.GetDocumentStore <CivilDocumentStore>();

            cds.PlotTypes.Add(PlotType.CurrentOpen);

            PlotType.CurrentOpen = null;
            OnCurrentOpenChanged?.Invoke();
        }
Пример #3
0
        public override void Run(string filename, Database db)
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                try
                {
                    // Get all items in this space
                    BlockTableRecord   btrModel = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
                    ObjectIdCollection items    = new ObjectIdCollection();
                    foreach (ObjectId id in btrModel)
                    {
                        items.Add(id);
                    }

                    if (items.Count > 0)
                    {
                        // Create the unexlodable anon block
                        BlockTableRecord btrProtected = new BlockTableRecord();
                        btrProtected.Name       = "*U";
                        btrProtected.Explodable = false;

                        // Add the new block to the block table
                        BlockTable bt    = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
                        ObjectId   btrId = bt.Add(btrProtected);
                        tr.AddNewlyCreatedDBObject(btrProtected, true);

                        // Add all entities in this space to the anon block
                        btrProtected.AssumeOwnershipOf(items);

                        // Add the block reference to this space
                        BlockReference br = new BlockReference(Autodesk.AutoCAD.Geometry.Point3d.Origin, btrId);
                        btrModel.AppendEntity(br);
                        tr.AddNewlyCreatedDBObject(br, true);
                    }
                }
                catch (System.Exception ex)
                {
                    OnError(ex);
                }

                tr.Commit();
            }
        }
Пример #4
0
        private static void ImportMBlock(string path, BlockTable blockTable)
        {
            using (Database mBlocksDb = new Database(false, true))
            {
                ObjectIdCollection oidc;

                mBlocksDb.ReadDwgFile(path, FileShare.Read, false, null);
                mBlocksDb.CloseInput(true);

                using (Transaction trans = mBlocksDb.TransactionManager.StartTransaction())
                {
                    BlockTable       bt         = (BlockTable)mBlocksDb.BlockTableId.GetObject(OpenMode.ForRead);
                    BlockTableRecord modelSpace = (BlockTableRecord)bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForRead);

                    using (BlockTableRecord btr = new BlockTableRecord())
                    {
                        bt.UpgradeOpen();
                        bt.Add(btr);
                        btr.Name = Regex.Match(path, @"M\d{4}").Value;

                        oidc = new ObjectIdCollection();
                        foreach (ObjectId oid in modelSpace)
                        {
                            oidc.Add(oid);
                        }

                        btr.AssumeOwnershipOf(oidc);
                        trans.AddNewlyCreatedDBObject(btr, true);

                        oidc = new ObjectIdCollection(new ObjectId[] { btr.ObjectId });
                    }

                    trans.Commit();
                }

                mBlocksDb.WblockCloneObjects(oidc, blockTable.ObjectId, new IdMapping(), DuplicateRecordCloning.Ignore, false);
            }
        }
Пример #5
0
        public ObjectId BlockDefinitionToNativeDB(BlockDefinition definition)
        {
            // get modified definition name with commit info
            var blockName = RemoveInvalidAutocadChars($"{Doc.UserData["commit"]} - {definition.name}");

            ObjectId blockId = ObjectId.Null;

            // see if block record already exists and return if so
            BlockTable blckTbl = Trans.GetObject(Doc.Database.BlockTableId, OpenMode.ForRead) as BlockTable;

            if (blckTbl.Has(blockName))
            {
                return(blckTbl[blockName]);
            }

            // create btr
            using (BlockTableRecord btr = new BlockTableRecord())
            {
                btr.Name = blockName;

                // base point
                btr.Origin = PointToNative(definition.basePoint);

                // add geometry
                blckTbl.UpgradeOpen();
                var bakedGeometry = new ObjectIdCollection(); // this is to contain block def geometry that is already added to doc space during conversion
                foreach (var geo in definition.geometry)
                {
                    if (CanConvertToNative(geo))
                    {
                        Entity converted = null;
                        switch (geo)
                        {
                        case BlockInstance o:
                            BlockInstanceToNativeDB(o, out BlockReference reference, false);
                            converted = reference;
                            break;

                        default:
                            converted = ConvertToNative(geo) as Entity;
                            break;
                        }

                        if (converted == null)
                        {
                            continue;
                        }
                        else if (!converted.IsNewObject && !(converted is BlockReference))
                        {
                            bakedGeometry.Add(converted.Id);
                        }
                        else
                        {
                            btr.AppendEntity(converted);
                        }
                    }
                }
                blockId = blckTbl.Add(btr);
                btr.AssumeOwnershipOf(bakedGeometry); // add in baked geo
                Trans.AddNewlyCreatedDBObject(btr, true);
                blckTbl.Dispose();
            }


            return(blockId);
        }
Пример #6
0
        public void Cmd_AutoBlock()
        {
            if (!LicensingAgent.Check())
            {
                return;
            }
            var acCurDoc = Application.DocumentManager.MdiActiveDocument;
            var acCurDb  = acCurDoc.Database;
            var acCurEd  = acCurDoc.Editor;

            var objIds = acCurEd.GetAllSelection(false);

            if (objIds.Length <= 0)
            {
                return;
            }

            var bNameTaken = true;
            var bName      = string.Empty;

            using (var acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // Open the Block table for read
                var acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite) as BlockTable;

                if (acBlkTbl == null)
                {
                    acTrans.Abort();
                    return;
                }

                while (bNameTaken)
                {
                    var checkName = acCurEd.GetSimpleString("\nEnter desired block name: ");

                    if (string.IsNullOrEmpty(checkName))
                    {
                        acTrans.Abort();
                        return;
                    }

                    if (acBlkTbl.Has(checkName))
                    {
                        acCurEd.WriteMessage("\nBlock name already exists.");
                        continue;
                    }

                    bName      = checkName;
                    bNameTaken = false;
                }

                var objCol = new ObjectIdCollection();

                foreach (var obj in objIds)
                {
                    objCol.Add(obj);
                }

                var extents = acTrans.GetExtents(objIds, acCurDb);

                var acBtr = new BlockTableRecord();

                acBtr.Name         = bName;
                acBtr.Origin       = extents.MinPoint;
                acBtr.BlockScaling = BlockScaling.Uniform;
                acBtr.Explodable   = true;
                acBtr.Units        = UnitsValue.Inches;

                #region TODO replace with xml reading

                ////Add attribute definitions
                ////TODO read XML to get attributes to add
                ////For now we'll use a test value

                //var attTag = new AttributeDefinition
                //{
                //    Justify = AttachmentPoint.MiddleCenter,
                //    AlignmentPoint = extents.MinPoint,
                //    Prompt = "TAG:",
                //    Tag = "TAG",
                //    TextString = bName,
                //    Height = 1,
                //    Invisible = true,
                //    LockPositionInBlock = true
                //};

                //var attCrate = new AttributeDefinition
                //{
                //    Justify = AttachmentPoint.MiddleCenter,
                //    AlignmentPoint = extents.MinPoint,
                //    Prompt = "CRATE:",
                //    Tag = "CRATE",
                //    TextString = "",
                //    Height = 1,
                //    Invisible = true,
                //    LockPositionInBlock = true
                //};

                #endregion

                var blockId = acBlkTbl.Add(acBtr);

                //acBtr.AppendEntity(attTag);
                //acBtr.AppendEntity(attCrate);
                acTrans.AddNewlyCreatedDBObject(acBtr, true);

                var map = new IdMapping();
                acCurDb.DeepCloneObjects(objCol, acBtr.ObjectId, map, false);
                var objCol2 = new ObjectIdCollection();

                foreach (IdPair pair in map)
                {
                    if (!pair.IsPrimary)
                    {
                        continue;
                    }
                    var ent = acTrans.GetObject(pair.Value, OpenMode.ForWrite) as Entity;

                    if (ent == null)
                    {
                        continue;
                    }
                    objCol2.Add(ent.ObjectId);
                }

                acBtr.AssumeOwnershipOf(objCol2);

                var acBr = new BlockReference(extents.MinPoint, blockId);
                acCurDb.AppendEntity(acBr);
                acBr.AppendAttributes(acBtr, acTrans);

                foreach (var obj in objIds)
                {
                    var acEnt = acTrans.GetObject(obj, OpenMode.ForWrite) as Entity;
                    if (acEnt == null)
                    {
                        continue;
                    }
                    acEnt.Erase();
                }

                acTrans.Commit();
            }

            acCurDb.Audit(true, false);
        }
Пример #7
0
        public static void AppendAttributeTest()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            Database db = doc.Database;

            try
            {
                using (doc.LockDocument())
                {
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                        BlockTableRecord currSp = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;


                        PromptNestedEntityOptions pno =

                            new PromptNestedEntityOptions("\nSelect source attribute to append new attribute below this one >>");

                        PromptNestedEntityResult nres =

                            ed.GetNestedEntity(pno);

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

                        ObjectId id = nres.ObjectId;

                        Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);

                        Point3d pnt = nres.PickedPoint;

                        ObjectId owId = ent.OwnerId;

                        AttributeReference attref = null;

                        if (id.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(AttributeReference))))
                        {
                            attref = tr.GetObject(id, OpenMode.ForWrite) as AttributeReference;
                        }


                        BlockTableRecord btr = null;

                        BlockReference bref = null;

                        if (owId.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(BlockReference))))
                        {
                            bref = tr.GetObject(owId, OpenMode.ForWrite) as BlockReference;

                            if (bref.IsDynamicBlock)
                            {
                                btr = tr.GetObject(bref.DynamicBlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
                            }
                            else
                            {
                                btr = tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
                            }
                        }

                        Point3d insPt = attref.Position.TransformBy(bref.BlockTransform);

                        btr.UpgradeOpen();

                        ObjectIdCollection bids = new ObjectIdCollection();

                        AttributeDefinition def = null;

                        foreach (ObjectId defid in btr)
                        {
                            if (defid.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(AttributeDefinition))))
                            {
                                def = tr.GetObject(defid, OpenMode.ForRead) as AttributeDefinition;

                                if (def.Tag == attref.Tag)
                                {
                                    def.UpgradeOpen();

                                    bids.Add(defid);

                                    break;
                                }
                            }
                        }



                        IdMapping map = new IdMapping();

                        db.DeepCloneObjects(bids, btr.ObjectId, map, true);

                        ObjectIdCollection coll = new ObjectIdCollection();

                        AttributeDefinition attDef = null;

                        foreach (IdPair pair in map)
                        {
                            if (pair.IsPrimary)
                            {
                                Entity oent = (Entity)tr.GetObject(pair.Value, OpenMode.ForWrite);

                                if (oent != null)
                                {
                                    if (pair.Value.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(AttributeDefinition))))
                                    {
                                        attDef = oent as AttributeDefinition;

                                        attDef.UpgradeOpen();

                                        attDef.SetPropertiesFrom(def as Entity);
                                        // add other properties from source attribute definition to suit here:

                                        attDef.Justify = def.Justify;

                                        attDef.Position = btr.Origin.Add(

                                            new Vector3d(attDef.Position.X, attDef.Position.Y - attDef.Height * 1.25, attDef.Position.Z)).TransformBy(Matrix3d.Identity

                                                                                                                                                      );

                                        attDef.Tag = "NEW_TAG";

                                        attDef.TextString = "New Prompt";

                                        attDef.TextString = "New Textstring";

                                        coll.Add(oent.ObjectId);
                                    }
                                }
                            }
                        }
                        btr.AssumeOwnershipOf(coll);

                        btr.DowngradeOpen();

                        attDef.Dispose();//optional

                        bref.RecordGraphicsModified(true);

                        tr.TransactionManager.QueueForGraphicsFlush();

                        doc.TransactionManager.FlushGraphics();//optional

                        ed.UpdateScreen();

                        tr.Commit();
                    }
                }
            }

            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
            }
            finally
            {
                AcAp.ShowAlertDialog("Call command \"ATTSYNC\" manually");
            }
        }