Example #1
0
        //private bool checkBlockRefs(BlockTableRecord btrFrame, Transaction t)
        //{
        //   bool res = false;
        //   var ms = t.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(_db), OpenMode.ForRead) as BlockTableRecord;
        //   foreach (ObjectId idEnt in ms)
        //   {
        //      if (idEnt.ObjectClass.Name == "AcDbBlockReference")
        //      {
        //         var blRef = t.GetObject(idEnt, OpenMode.ForRead, false, true) as BlockReference;
        //         if (blRef.GetEffectiveName().Equals(_blFrameName, StringComparison.OrdinalIgnoreCase))
        //         {
        //            // считывание атрибутов
        //            var atrCol = blRef.AttributeCollection;
        //            _attrs = new Dictionary<string, string>();
        //            foreach (ObjectId idAtrRef in atrCol)
        //            {
        //               var atrRef = t.GetObject(idAtrRef, OpenMode.ForRead, false, true) as AttributeReference;
        //               string key = atrRef.Tag.ToUpper();
        //               if (!_attrs.ContainsKey(key))
        //               {
        //                  _attrs.Add(key, atrRef.TextString);
        //               }
        //            }
        //            res = true;
        //         }
        //      }
        //   }
        //   return res;
        //}
        //private bool checkBtrFrame(BlockTableRecord btrFrame, Transaction t)
        //{
        //   bool res = false;
        //   if (btrFrame.HasAttributeDefinitions)
        //   {
        //      Dictionary<string, string> attrsChecks = new Dictionary<string, string>();
        //      foreach (ObjectId idEnt in btrFrame)
        //      {
        //         if (idEnt.ObjectClass.Name == "AcDbAttributeDefinition")
        //         {
        //            var atrDef = t.GetObject(idEnt, OpenMode.ForRead, false, true) as AttributeDefinition;
        //            switch (atrDef.Tag.ToUpper())
        //            {
        //               case "ВИД":
        //                  attrsChecks.Add("ВИД", atrDef.TextString);
        //                  break;
        //               case "НАИМЕНОВАНИЕ":
        //                  attrsChecks.Add("НАИМЕНОВАНИЕ", atrDef.TextString);
        //                  break;
        //               case "ЛИСТ":
        //                  attrsChecks.Add("ЛИСТ", atrDef.TextString);
        //                  break;
        //               default:
        //                  break;
        //            }
        //         }
        //      }
        //      if (attrsChecks.Count == 3)
        //      {
        //         res = true;
        //      }
        //   }
        //   return res;
        //}
        private void updateBlRefFrame(BlockReference blRef, BlockTableRecord btrFrame, Transaction t, bool insertDescription)
        {
            // Обновление вхождения блока рамки
             if (!IsOk)
            return;

             // Удаление атрибутов
             foreach (ObjectId idAtrRef in blRef.AttributeCollection)
             {
            var atrRef = t.GetObject(idAtrRef, OpenMode.ForWrite, false, true) as AttributeReference;
            atrRef.Erase(true);
             }

             blRef.UpgradeOpen();
             foreach (ObjectId idEnt in btrFrame)
             {
            if (idEnt.ObjectClass.Name == "AcDbAttributeDefinition")
            {
               var atrDef = t.GetObject(idEnt, OpenMode.ForRead, false, true) as AttributeDefinition;
               if (!atrDef.Constant)
               {
                  if (!insertDescription && atrDef.Tag.Equals("ПРИМЕЧАНИЕ", StringComparison.OrdinalIgnoreCase))
                  {
                     continue;
                  }
                  using (var atrRef = new AttributeReference())
                  {
                     atrRef.SetAttributeFromBlock(atrDef, blRef.BlockTransform);
                     string key = atrDef.Tag.ToUpper();
                     if (_attrs.ContainsKey(key))
                     {
                        atrRef.TextString = _attrs[key];
                     }
                     else
                     {
                        atrRef.TextString = atrDef.TextString;
                     }
                     blRef.AttributeCollection.AppendAttribute(atrRef);
                     t.AddNewlyCreatedDBObject(atrRef, true);
                  }
               }
            }
             }
        }
Example #2
0
        public override void Run(string filename, Database db)
        {
            if (blockNames.Count == 0)
            {
                return;
            }

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                try
                {
                    ObjectIdCollection blockIDs = new ObjectIdCollection();

                    if (applyToAllBlockDefinitons)
                    {
                        // Apply to all block definitions
                        BlockTable blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                        foreach (ObjectId id in blockTable)
                        {
                            if (id == SymbolUtilityServices.GetBlockModelSpaceId(db))
                            {
                                continue;
                            }

                            BlockTableRecord block = (BlockTableRecord)tr.GetObject(id, OpenMode.ForRead);
                            if (block.IsLayout)
                            {
                                continue;
                            }

                            blockIDs.Add(id);
                        }
                    }
                    else
                    {
                        // Model space (or entire drawing)
                        if (applyModel)
                        {
                            blockIDs.Add(SymbolUtilityServices.GetBlockModelSpaceId(db));
                        }

                        // Layouts (or entire drawing)
                        if (applyLayouts)
                        {
                            DBDictionary layoutDict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
                            foreach (DBDictionaryEntry entry in layoutDict)
                            {
                                if (entry.Key.ToUpperInvariant() != "MODEL")
                                {
                                    Layout layout = (Layout)tr.GetObject(entry.Value, OpenMode.ForRead);
                                    blockIDs.Add(layout.BlockTableRecordId);
                                }
                            }
                        }
                    }

                    foreach (ObjectId spaceId in blockIDs)
                    {
                        // Open the block space
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(spaceId, OpenMode.ForRead);
                        foreach (ObjectId id in btr)
                        {
                            if (id.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(BlockReference))))
                            {
                                BlockReference bref      = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
                                bool           canDelete = false;
                                foreach (string name in blockNames)
                                {
                                    if (string.Compare(name, bref.Name, StringComparison.OrdinalIgnoreCase) == 0)
                                    {
                                        canDelete = true;
                                    }
                                }
                                if (canDelete)
                                {
                                    bref.UpgradeOpen();
                                    bref.Erase(true);
                                }
                            }
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    OnError(ex);
                }

                tr.Commit();
            }
        }
        public static Boolean HasAttributes(BlockReference blkRef, String attName, out AttributeReference attRf,
                                            Document doc, Transaction tr)
        {
            IEnumerable <AttributeDefinition> attDefs; //Los atributos definidos en el registro de bloque
            IEnumerable <AttributeReference>  attRefs; //Los atributos referenciados en el bloque
            AttributeCollection attColl;

            //Si esta en modo lectura se cambia a modo escritura
            if (blkRef.IsReadEnabled)
            {
                blkRef.UpgradeOpen();
            }
            BlockTableRecord block = (BlockTableRecord)
                                     blkRef.BlockTableRecord.GetObject(OpenMode.ForRead);

            //Atributos existentes en el bloque
            attColl = blkRef.AttributeCollection;
            //Seleccionamos las definiciones de atributos del registro de bloque
            //LINQ para seleccionar los atributos
            attDefs = block.OfType <ObjectId>().Select(
                x =>
            {
                DBObject obj = x.GetObject(OpenMode.ForRead);
                if (obj is AttributeDefinition)
                {
                    return((AttributeDefinition)obj);
                }
                else
                {
                    return(null);
                }
            }).Where(y => y != null);
            if (attDefs.Count() > 0)
            {
                //Obtengo mis atributos referenciados
                attRefs = attColl == null ? new AttributeReference[0] :
                          attColl.OfType <ObjectId>().Select(x =>
                {
                    DBObject obj = x.GetObject(OpenMode.ForRead);
                    if (obj is AttributeReference)
                    {
                        return((AttributeReference)obj);
                    }
                    else
                    {
                        return(null);
                    }
                }).Where(y => y != null);
                attRf = attRefs.FirstOrDefault(x => x.Tag == attName);
                if (attRf != null)
                {
                    return(true);
                }
                else
                {
                    var attDef = attDefs.FirstOrDefault(x => x.Tag == attName);
                    if (attDef != null)
                    {
                        attRf = new AttributeReference();
                        attRf.SetAttributeFromBlock(attDef, blkRef.BlockTransform);
                        blkRef.AttributeCollection.AppendAttribute(attRf);
                        tr.AddNewlyCreatedDBObject(attRf, true);
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            else
            {
                attRf = null;
                return(false);
            }
        }
Example #4
0
        public void ReplaceBlockReferenceWithBlockTableRecord(DocumentCollection acDocMgr, Document acDoc, Transaction acTrans, BlockTableRecord acBlkTblRecSpc, BlockReference acBlkRefRemoved,
                                                              BlockTableRecord acBklTblRecReplaced, int[] acAttRefIndexCol)
        {
            BlockTableRecord acBlkTblRecReplacedClone;
            BlockTable       acBlkTbl = (BlockTable)acTrans.GetObject(acDoc.Database.BlockTableId, OpenMode.ForRead);

            //BlockTableRecord acBlkTblRecReplacedClone = (BlockTableRecord)acBklTblRecReplaced.Clone();
            if (!acBlkTbl.IsWriteEnabled)
            {
                acBlkTbl.UpgradeOpen();
            }
            if (!acBlkTbl.Has(acBklTblRecReplaced.Name))
            {
                //acBlkTbl.Add(acBlkTblRecReplacedClone); acTrans.AddNewlyCreatedDBObject(acBlkTblRecReplacedClone, true);
                InsertBlock(acDocMgr, acDoc, acBklTblRecReplaced);
            }
            acBlkTblRecReplacedClone = (BlockTableRecord)acTrans.GetObject(acBlkTbl[acBklTblRecReplaced.Name], OpenMode.ForRead);
            //Application.ShowAlertDialog("Start");
            //BlockTableRecord acBlkTblRecRemoved =(BlockTableRecord) acTrans.GetObject(acBlkRefRemoved.BlockTableRecord, OpenMode.ForRead);
            Extents3d acExtRemoved = acBlkRefRemoved.GeometricExtents;
            //Extents3d extents = br.GeometricExtents;
            //Vector3d acVec3d = acBlkTblRecRemoved.Origin.GetVectorTo(acExtRemoved.MinPoint);
            //Application.ShowAlertDialog("-2");
            double acLenRemoved = Math.Abs(acExtRemoved.MinPoint.Y - acExtRemoved.MaxPoint.Y);
            //Application.ShowAlertDialog(acLenRemoved.ToString());
            //Application.ShowAlertDialog("-3");
            BlockReference acBlkRefReplacedIns = new BlockReference(new Point3d(0, 0, 0), acBlkTblRecReplacedClone.Id);

            //Application.ShowAlertDialog("-4");
            Extents3d acExtReplaced = acBlkRefReplacedIns.GeometricExtents;
            //acVec3d = acBklTblRecReplaced.Origin.GetVectorTo(acExtReplaced.MinPoint);
            double acLenReplaced = Math.Abs(acExtReplaced.MinPoint.Y - acExtReplaced.MaxPoint.Y);
            //Application.ShowAlertDialog(acLenReplaced.ToString());
            //Point3d acPtBot = new Point3d(acBlkRefRemoved.Position.X + acVec3d.X* acBlkRefRemoved.ScaleFactors.X, acBlkRefRemoved.Position.Y + acVec3d.Y* acBlkRefRemoved.ScaleFactors.Y, 0);
            Point3d acPtBot = acExtRemoved.MinPoint;
            Point3d acPtPos = new Point3d(acPtBot.X - acExtReplaced.MinPoint.X, acPtBot.Y - acExtReplaced.MinPoint.Y, 0);

            //Application.ShowAlertDialog("-5");

            string[] acAttRefValCol = GetAttributeReferenceValueCollection(acTrans, acBlkRefRemoved);

            /*
             * for (int j = 0; j < acAttRefValCol.Length; j++)
             * {
             *  Application.ShowAlertDialog(acAttRefValCol[j]);
             *  Application.ShowAlertDialog(acAttRefIndexCol[j].ToString());
             * }
             */
            //Application.ShowAlertDialog("-6");
            BlockReference acBlkRefReplaced = new BlockReference(acPtPos, acBlkTblRecReplacedClone.Id);

            //Application.ShowAlertDialog("-7");
            if (!acBlkTblRecSpc.IsWriteEnabled)
            {
                acBlkTblRecSpc.UpgradeOpen();
            }
            //Application.ShowAlertDialog("1");
            acBlkTblRecSpc.AppendEntity(acBlkRefReplaced);
            acTrans.AddNewlyCreatedDBObject(acBlkRefReplaced, true);

            if (!acBlkRefRemoved.IsWriteEnabled)
            {
                acBlkRefRemoved.UpgradeOpen();
            }
            acBlkRefRemoved.Erase(true);
            //Application.ShowAlertDialog("2");
            int i = 0;

            foreach (ObjectId acObjId in acBlkTblRecReplacedClone)
            {
                //Application.ShowAlertDialog("Index \n"+i.ToString());
                DBObject acDbObj = acTrans.GetObject(acObjId, OpenMode.ForRead);
                if (acDbObj is AttributeDefinition)
                {
                    //Application.ShowAlertDialog("3");
                    AttributeDefinition acAttDef = (AttributeDefinition)acDbObj;
                    if (!acAttDef.Constant)
                    {
                        using (AttributeReference acAttRef = new AttributeReference())
                        {
                            if (!acBlkTblRecReplacedClone.IsWriteEnabled)
                            {
                                acBlkTblRecReplacedClone.UpgradeOpen();
                            }
                            if (!acBlkRefReplaced.IsWriteEnabled)
                            {
                                acBlkRefReplaced.UpgradeOpen();
                            }
                            //Application.ShowAlertDialog("4");
                            acAttRef.SetAttributeFromBlock(acAttDef, acBlkRefReplaced.BlockTransform);
                            acAttRef.Position = acAttDef.Position.TransformBy(acBlkRefReplaced.BlockTransform);
                            //Application.ShowAlertDialog("5");
                            if (i < acAttRefIndexCol.Length)
                            {
                                if (acAttRefIndexCol[i] < acAttRefIndexCol.Length)
                                {
                                    acAttRef.TextString = acAttRefValCol[acAttRefIndexCol[i]];
                                }
                                else
                                {
                                    acAttRef.TextString = acAttDef.TextString;
                                }
                            }
                            else
                            {
                                acAttRef.TextString = acAttDef.TextString;
                            }
                            Application.ShowAlertDialog(acAttRef.TextString);
                            //Application.ShowAlertDialog("6");
                            acBlkRefReplaced.AttributeCollection.AppendAttribute(acAttRef);
                            acTrans.AddNewlyCreatedDBObject(acAttRef, true);
                            //Application.ShowAlertDialog("7");
                            i++;
                            //Application.ShowAlertDialog("Index \n"+i.ToString());
                        }
                    }
                }
            }
            //Application.ShowAlertDialog("8");
            //double acScl = acLenRemoved / acLenReplaced * ((double)acBlkRefRemoved.ScaleFactors.Y);
            //acScl = acLenRemoved / acLenReplaced;
            //Application.ShowAlertDialog(acScl.ToString());
            //double acSCL1 = 1;
            acBlkRefReplaced.TransformBy(Matrix3d.Scaling(acLenRemoved / acLenReplaced /*acBlkRefRemoved.ScaleFactors.X*/, acPtBot));

            //tBlkRef.TransformBy(Matrix3d.Scaling(acScale, tBlkRef.Position));
            //Application.ShowAlertDialog("Finish");
        }
Example #5
0
        private static void ReplaceBlock(BlockReference element, BlockTable blockTable, BlockTableRecord modelSpace)
        {
            // If entity is a TRP####(mapit) block replace it with a mavat(M####) block.
            if ((Regex.IsMatch(element.Name, "TRP.")))
            {
                Dictionary <string, string> attDic = new Dictionary <string, string>();
                foreach (ObjectId oid in element.AttributeCollection)
                {
                    AttributeReference ar = (AttributeReference)oid.GetObject(OpenMode.ForRead);
                    if (ar.Tag == "HIGHTM")
                    {
                        attDic.Add("HEIGHT", ar.TextString);
                    }
                    else
                    {
                        attDic.Add(ar.Tag, ar.TextString);
                    }
                }

                if (!trpToMavat.Keys.Contains(element.Name))
                {
                    QuaryConvertTable(element.Name, blockTable);
                }

                using (Transaction trans = dataBase.TransactionManager.StartTransaction())
                {
                    try
                    {
                        // btr = BlockTableRecord for mavat block in database.
                        BlockTableRecord btr = (BlockTableRecord)blockTable[trpToMavat[element.Name]].GetObject(OpenMode.ForRead);

                        // Append mavat block to drawing.
                        using (BlockReference br = new BlockReference(element.Position, btr.ObjectId))
                        {
                            modelSpace.UpgradeOpen();
                            modelSpace.AppendEntity(br);
                            element.UpgradeOpen();
                            element.Erase();

                            // Add all attributes to new mavat block.
                            foreach (ObjectId id in btr)
                            {
                                var v = id.GetObject(OpenMode.ForRead);

                                if (v.GetType() == typeof(AttributeDefinition))
                                {
                                    using (AttributeReference attRef = new AttributeReference())
                                    {
                                        attRef.SetAttributeFromBlock((AttributeDefinition)v, br.BlockTransform);
                                        attRef.TextString = attDic[attRef.Tag];
                                        br.AttributeCollection.AppendAttribute(attRef);
                                        trans.AddNewlyCreatedDBObject(attRef, true);
                                    }
                                }
                            }

                            trans.AddNewlyCreatedDBObject(br, true);
                        }

                        trans.Commit();
                    }
                    catch (KeyNotFoundException e)
                    {
                        doc.Editor.WriteMessage($"{element.Name} does not exists in trpToMavat dictionary.\n");
                    }
                }
            }
        }