public static void ReplaceAttributeBlock(AcDb.ObjectId idBlock,
                                                 string tag,
                                                 string newValue,
                                                 Boolean visible)
        {
            try
            {
                //using (AcDb.Transaction tr = db.TransactionManager.StartTransaction())
                //{
                //    DBObject dbObj = tr.GetObject(idBlock, AcDb.OpenMode.ForRead) as DBObject;
                //    AcDb.BlockReference blockReference = dbObj as BlockReference;
                //    ReplaceAttributeBlock(blockReference, tag, newValue, visible);
                //}


                using (AcDb.Transaction tr = db.TransactionManager.StartTransaction())
                {
                    AcDb.BlockTable acBlockTable = (AcDb.BlockTable)tr.GetObject(db.BlockTableId, AcDb.OpenMode.ForRead);
                    if (acBlockTable == null)
                    {
                        return;
                    }

                    AcDb.BlockTableRecord acBlockTableRecord = (AcDb.BlockTableRecord)
                                                               tr.GetObject(acBlockTable[AcDb.BlockTableRecord.ModelSpace], AcDb.OpenMode.ForRead);
                    if (acBlockTableRecord == null)
                    {
                        return;
                    }

                    //foreach (var blkId in acBlockTableRecord)
                    //{
                    AcDb.BlockReference acBlock = (AcDb.BlockReference)tr.GetObject(idBlock, AcDb.OpenMode.ForRead);
                    //if (acBlock == null) continue;
                    //if (!acBlock.Name.Equals(blockName, StringComparison.CurrentCultureIgnoreCase)) continue;
                    foreach (AcDb.ObjectId attId in acBlock.AttributeCollection)
                    {
                        AcDb.AttributeReference acAtt = (AcDb.AttributeReference)tr.GetObject(attId, AcDb.OpenMode.ForRead);
                        if (acAtt == null)
                        {
                            continue;
                        }

                        if (!acAtt.Tag.Equals(tag, StringComparison.CurrentCultureIgnoreCase))
                        {
                            continue;
                        }

                        acAtt.UpgradeOpen();
                        acAtt.TextString = newValue;
                    }
                    //}
                    tr.Commit();
                }
            }
            catch //(System.Exception exc)
            {
            }
        }
        public static void ReplaceAttributeBlock(AcDb.BlockReference blockReference,
                                                 Dictionary <string, string> tags,
                                                 Boolean visible)
        {
            using (AcDb.Transaction trAdding = db.TransactionManager.StartTransaction())
            {
                AcDb.BlockTable       btTable  = (AcDb.BlockTable)trAdding.GetObject(db.BlockTableId, AcDb.OpenMode.ForRead);
                AcDb.BlockTableRecord btrTable =
                    (AcDb.BlockTableRecord)btTable[blockReference.Name].GetObject(AcDb.OpenMode.ForRead);

                if (btrTable.HasAttributeDefinitions)
                {
                    var attDefs = btrTable.Cast <AcDb.ObjectId>()
                                  .Where(n => n.ObjectClass.Name == "AcDbAttributeDefinition")
                                  .Select(n => (AcDb.AttributeDefinition)n.GetObject(AcDb.OpenMode.ForRead));

                    foreach (AcDb.AttributeDefinition attDef in attDefs)
                    {
                        AcDb.AttributeReference attRef = new AcDb.AttributeReference();
                        attRef.SetAttributeFromBlock(attDef, blockReference.BlockTransform);

                        foreach (var tag in tags)
                        {
                            attRef.UpgradeOpen();
                            if (tag.Key.ToUpper() == attRef.Tag)
                            {
                                attRef.TextString = tag.Value;
                            }
                            else
                            {
                                attRef.TextString = "NoneValue";
                            }
                            attRef.Visible  = visible;
                            attRef.Rotation = 0;
                        }

                        blockReference.AttributeCollection.AppendAttribute(attRef);
                        trAdding.AddNewlyCreatedDBObject(attRef, false);
                    }
                }
                else
                {
                    ed.WriteMessage("\n < ERROR > ");
                }

                trAdding.Commit();
            }
        }
Exemple #3
0
        private bool Import(List <Excelizer.ImportedRow> importedRows)
        {
            bool errorOccured = false;

            var doc = _AcAp.Application.DocumentManager.MdiActiveDocument;
            var db  = doc.Database;

            using (_AcDb.Transaction trans = doc.TransactionManager.StartTransaction())
            {
                var perBlockName = importedRows.GroupBy(x => x.BlockName);
                foreach (var pbn in perBlockName)
                {
                    int nrOfChangedBlocks = 0;

                    string blockName = pbn.Key;
                    if (blockName == null || string.IsNullOrEmpty(blockName.Trim()))
                    {
                        log.WarnFormat("Es gibt Zeilen ohne Blocknamen!");
                        continue;
                    }

                    var attNames = Plan2Ext.Globs.GetBlockAttributeNames(blockName);
                    log.InfoFormat("Import für Block '{0}' mit {1} Attribut(en): ", blockName, attNames.Count.ToString());
                    int cnt = 0;
                    foreach (var record in pbn)
                    {
                        try
                        {
                            int changedAttributes = 0;

                            long ln = Convert.ToInt64(record.Handle, 16);
                            // Not create a Handle from the long integer
                            _AcDb.Handle hn = new _AcDb.Handle(ln);
                            // And attempt to get an _AcDb.ObjectId for the Handle
                            _AcDb.ObjectId id = db.GetObjectId(false, hn, 0);

                            _AcDb.BlockReference br = (_AcDb.BlockReference)trans.GetObject(id, _AcDb.OpenMode.ForWrite);

                            cnt++;

                            foreach (var attName in attNames)
                            {
                                _AcDb.AttributeReference attRef = null;
                                attRef = GetBlockAttribute(attName, br, trans);
                                if (attRef != null)
                                {
                                    attRef.UpgradeOpen();
                                    string attVal = record.GetAttribute(attName);
                                    if (attVal != null)
                                    {
                                        if (string.Compare(attRef.TextString, attVal) != 0)
                                        {
                                            if (_CreateChangeLines)
                                            {
                                                Plan2Ext.Globs.InsertFehlerLines(new List <_AcGe.Point3d> {
                                                    attRef.Position
                                                },
                                                                                 "Plan2BlockAttribChanged",
                                                                                 50.0,
                                                                                 Math.PI * 1.25,
                                                                                 _AcCm.Color.FromRgb((byte)255, (byte)0, (byte)0));
                                            }

                                            changedAttributes++;
                                        }
                                        attRef.TextString = attVal;
                                    }
                                }
                            }
                            if (changedAttributes > 0)
                            {
                                log.DebugFormat("Block mit Referenz '{0}': Anzahl geänderter Attribute: {1}", record.Handle, changedAttributes.ToString());
                                nrOfChangedBlocks++;
                            }
                        }
                        catch (Exception ex)
                        {
                            log.Warn(string.Format("Fehler beim Import von Block mit Handle '{0}'! {1}", record.Handle, ex.Message));
                            errorOccured = true;
                        }
                    }

                    log.InfoFormat("Anzahl geänderter Blöcke namens '{0}': {1}/{2}", blockName, nrOfChangedBlocks, cnt);
                }
                trans.Commit();
            }
            return(!errorOccured);
        }