/// <summary> /// Realizá la carga del bloque /// </summary> /// <param name="tr">La transacción activa.</param> /// <param name="localDwg">La base de datos local.</param> /// <param name="file">La ruta del bloque a cargar.</param> /// <param name="blockName">El nombre del bloque.</param> public static void LoadBlock(this Transaction tr, Database localDwg, string file, string blockName) { BlockTable blkTab = (BlockTable)localDwg.BlockTableId.GetObject(OpenMode.ForRead); if (!blkTab.Has(blockName) && File.Exists(file)) { blkTab.UpgradeOpen(); BlockTableRecord newRecord = new BlockTableRecord(); newRecord.Name = blockName; blkTab.Add(newRecord); tr.AddNewlyCreatedDBObject(newRecord, true); //3: Se abre la base de datos externa Database externalDB = new Database(); externalDB.ReadDwgFile(file, FileShare.Read, true, null); //4: Con una subtransacción se clonán los elementos que esten contenidos en el espcio de modelo de la //base de datos externa ObjectIdCollection ids; using (Transaction subTr = externalDB.TransactionManager.StartTransaction()) { //4.1: Abrir el espacio de modelo de la base de datos externa ObjectId modelId = SymbolUtilityServices.GetBlockModelSpaceId(externalDB); BlockTableRecord model = subTr.GetObject(modelId, OpenMode.ForRead) as BlockTableRecord; //4.2: Se extraen y clonan los elementos mediante la clase IdMapping ids = new ObjectIdCollection(model.OfType <ObjectId>().ToArray()); using (IdMapping iMap = new IdMapping()) localDwg.WblockCloneObjects(ids, newRecord.Id, iMap, DuplicateRecordCloning.Replace, false); } } }
/// <summary> /// Loads a block table record from an external database to the current database. /// If the blockname exists on the table record, the block table record id is taken from the curren database block table. /// </summary> /// <param name="blockname">The block table record name.</param> /// <param name="filePath">The dwg block file path.</param> /// <param name="tr">The active transaction.</param> /// <returns>The object id of the block table record</returns> public static ObjectId _LoadBlock(this String blockname, String filePath, Transaction tr) { ObjectId id = new ObjectId(); Database dwg = Application.DocumentManager.MdiActiveDocument.Database; try { BlockTable blkTab = (BlockTable)dwg.BlockTableId.GetObject(OpenMode.ForRead); if (!blkTab.Has(blockname)) { //1: Se crea un registro de bloque blkTab.UpgradeOpen(); BlockTableRecord newRecord = new BlockTableRecord(); newRecord.Name = blockname; //2: Se agregá el registro a la tabla blkTab.Add(newRecord); tr.AddNewlyCreatedDBObject(newRecord, true); //3: Se abre la base de datos externa Database externalDB = new Database(false, true); externalDB.ReadDwgFile(filePath, FileShare.Read, true, ""); //4: Con una subtransacción se clonán los elementos que esten contenidos en el espcio de modelo de la //base de datos externa ObjectIdCollection ids; using (Transaction subTr = externalDB.TransactionManager.StartTransaction()) { //4.1: Abrir el espacio de modelo de la base de datos externa ObjectId modelId = SymbolUtilityServices.GetBlockModelSpaceId(externalDB); BlockTableRecord model = subTr.GetObject(modelId, OpenMode.ForRead) as BlockTableRecord; //4.2: Se extraen y clonan los elementos mediante la clase IdMapping ids = new ObjectIdCollection(model.OfType <ObjectId>().ToArray()); //IEnumerable<DBObject> objs = ids.OfType<ObjectId>().Select(x => x.GetObject(OpenMode.ForRead)); int erased = ids.OfType <ObjectId>().Count(x => x.IsValid); using (IdMapping iMap = new IdMapping()) dwg.WblockCloneObjects(ids, newRecord.Id, iMap, DuplicateRecordCloning.Replace, false); } id = newRecord.Id; } else { id = blkTab[blockname]; } } catch (Exception exc) { throw exc; } return(id); }
public static Boolean LoadBlock(String path, String blockname, Document doc, Transaction tr) { if (File.Exists(path)) { //Buscamos que no exista el registro BlockTable blkTab = (BlockTable) doc.Database.BlockTableId.GetObject(OpenMode.ForRead); //Si no existe el registro debe cargarse del archivo if (!blkTab.Has(blockname)) { //1: Creación de espacio de bloque blkTab.UpgradeOpen();//A modo escritura para escribir el bloque BlockTableRecord newRecord = new BlockTableRecord(); newRecord.Name = blockname; blkTab.Add(newRecord); tr.AddNewlyCreatedDBObject(newRecord, true); //2: Abrir base de datos externas Database external = new Database(); external.ReadDwgFile(path, FileShare.Read, true, null); ObjectIdCollection ids; //3: Agregarles las entidades mediante un mapeo using (Transaction extTr = external.TransactionManager.StartTransaction()) { BlockTableRecord extModelSpace = (BlockTableRecord) SymbolUtilityServices.GetBlockModelSpaceId(external).GetObject(OpenMode.ForRead); ids = new ObjectIdCollection(extModelSpace.OfType <ObjectId>().ToArray()); using (IdMapping iMap = new IdMapping()) { doc.Database.WblockCloneObjects(ids, newRecord.Id, iMap, DuplicateRecordCloning.Replace, false); } } } return(true); } else { BlockTable blkTab = (BlockTable)doc.Database.BlockTableId.GetObject(OpenMode.ForRead); //Si no existe el registro debe cargarse del archivo return(blkTab.Has(blockname)); } }
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); } }