public static void ChangeNameBR() { // Get the current document editor Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor; Database db = Application.DocumentManager.MdiActiveDocument.Database; // Create a TypedValue array to define the filter criteria TypedValue[] acTypValAr = new TypedValue[1]; acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "MTEXT"), 0); // Assign the filter criteria to a SelectionFilter object SelectionFilter acSelFtr = new SelectionFilter(acTypValAr); // Request for objects to be selected in the drawing area PromptSelectionResult acSSPrompt = acDocEd.GetSelection(acSelFtr); // If the prompt status is OK, objects were selected if (acSSPrompt.Status == PromptStatus.OK) { SelectionSet acSSet = acSSPrompt.Value; SelectedObject[] chosenMtextArr = new SelectedObject[1]; acSSet.CopyTo(chosenMtextArr, 0); Transaction trans = db.TransactionManager.StartTransaction(); MText mtext = trans.GetObject(chosenMtextArr[0].ObjectId, OpenMode.ForRead, true) as MText; string mtextText = mtext.Contents.ToString(); Application.ShowAlertDialog("Selected object: " + mtextText); //Block reference PromptEntityResult promptEntity = acDocEd.GetEntity("Choose a Block Reference"); if (acSSPrompt.Status != PromptStatus.OK) { acDocEd.WriteMessage("Block Reference choosing failed!"); return; } string attbName = "HEIGHT"; BlockReference blockReference = trans.GetObject(promptEntity.ObjectId, OpenMode.ForRead, true) as BlockReference; foreach (ObjectId arId in blockReference.AttributeCollection) { AttributeReference ar = trans.GetObject(arId, OpenMode.ForRead) as AttributeReference; if (null == ar) { acDocEd.WriteMessage("AttributeReference getting failed!"); return; } if (ar.Tag.ToUpper() == attbName) { ar.UpgradeOpen(); ar.TextString = mtextText; ar.DowngradeOpen(); } } trans.Commit(); } else { Application.ShowAlertDialog("Number of objects selected: 0"); } }
private void ChangeAttributeValue(Transaction trx, Database db, string blockName, string attributeTag, string oldString, string newString) { ObjectId psId; BlockTable bt = (BlockTable)trx.GetObject(db.BlockTableId, OpenMode.ForRead); psId = bt[BlockTableRecord.PaperSpace]; BlockTableRecord btr = (BlockTableRecord)trx.GetObject(psId, OpenMode.ForRead); foreach (var entId in btr) { Entity ent = trx.GetObject(entId, OpenMode.ForRead) as Entity; if (ent != null) { BlockReference br = ent as BlockReference; if (br != null) { BlockTableRecord bd = (BlockTableRecord)trx.GetObject(br.BlockTableRecord, OpenMode.ForRead); // ... to see whether it's a block with the name we're after if (bd.Name.ToUpper() == blockName.ToUpper()) { // Check each of the attributes... foreach (ObjectId arId in br.AttributeCollection) { DBObject obj = trx.GetObject(arId, OpenMode.ForRead); AttributeReference ar = obj as AttributeReference; if (ar != null) { // ... to see whether it has // the tag we're after if (ar.Tag.ToUpper() == attributeTag.ToUpper()) { // check if attribute has correct value if (ar.TextString == oldString) { // If so, update the value ar.UpgradeOpen(); ar.TextString = newString; ar.DowngradeOpen(); } } } } } } } } }
//same as the UpdateAttributesinBlock function I have in the Attribute class, but I had to copy it over here for slight changes //here it gets the Rev number in the title block and adds that to the attribute in the rev triangle block private static void UpdateAttributesInRevBlock(string blockName, string attbName, string attbValue) { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; Transaction tr = doc.TransactionManager.StartTransaction(); using (tr) { //get the current space BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead); // Test each entity in the container... foreach (ObjectId entId in btr) { Entity ent = tr.GetObject(entId, OpenMode.ForRead) as Entity; if (ent != null) { BlockReference br = ent as BlockReference; if (br != null) { BlockTableRecord bd = (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead); // ... to see whether it's a block with // the name we're after if (bd.Name.ToUpper() == blockName) { // Check each of the attributes... foreach (ObjectId arId in br.AttributeCollection) { DBObject obj = tr.GetObject(arId, OpenMode.ForRead); AttributeReference ar = obj as AttributeReference; if (ar != null) { // ... to see whether it has // the tag we're after if (ar.Tag.ToUpper() == attbName) { ar.UpgradeOpen(); ar.TextString = attbValue; ar.DowngradeOpen(); } } } } } } } tr.Commit(); } }
private void UpdateAttributesInBlock(ObjectId btrId, string blockName, Dictionary <string, string> attributeTagValue) { Document doc = Application.DocumentManager.MdiActiveDocument; using (Transaction trans = doc.TransactionManager.StartTransaction()) { BlockTableRecord btr = (BlockTableRecord)trans.GetObject(btrId, OpenMode.ForRead); foreach (ObjectId entId in btr) { Entity ent = trans.GetObject(entId, OpenMode.ForRead) as Entity; if (ent != null) { BlockReference br = ent as BlockReference; if (br != null) { BlockTableRecord btrec = (BlockTableRecord)trans.GetObject(br.BlockTableRecord, OpenMode.ForRead); if (string.Compare(btrec.Name, blockName, true) == 0) { foreach (ObjectId arObjectIdId in br.AttributeCollection) { DBObject obj = trans.GetObject(arObjectIdId, OpenMode.ForRead); AttributeReference ar = obj as AttributeReference; if (ar != null) { foreach (KeyValuePair <string, string> kvp in attributeTagValue) { if (string.Compare(ar.Tag, kvp.Key, true) == 0) { ar.UpgradeOpen(); ar.TextString = kvp.Value; ar.DowngradeOpen(); } } } } } // Recurse for nested blocks UpdateAttributesInBlock(br.BlockTableRecord, blockName, attributeTagValue); } } } trans.Commit(); } }
private BlockReference CreateBlockRef(DocumentModifier docMdf, BlockTableRecord space, BlockTableRecord blockDef, AttributeDefinition attDef, Point3d position) { BlockReference bref = new BlockReference(position, blockDef.Id); space.AppendEntity(bref); docMdf.acTransaction.AddNewlyCreatedDBObject(bref, true); // AttributeReference ar = new AttributeReference(attDef.Position.Add(position.GetAsVector()), position.Z.ToString(), BlockAttributeName, style: attDef.TextStyleId); bref.AttributeCollection.AppendAttribute(ar); ar.DowngradeOpen(); // AttributeReference 在 DowngradeOpen 后,才会立即显示,否则要通过Refresh等方法使其 DowngradeOpen 才能显示。 bref.DowngradeOpen(); return(bref); }
private void buttonOK_Click(object sender, EventArgs e) { Dispose(); Document doc = AcadApp.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; using (Transaction trans = db.TransactionManager.StartTransaction()) { //选择要刷新的块,可以任选,挑出其中的同名快 PromptSelectionOptions tgtOpt = new PromptSelectionOptions(); tgtOpt.MessageForAdding = "选择要刷新的块参照"; PromptSelectionResult tgtRes = ed.GetSelection(tgtOpt); ObjectId[] tgtIds = tgtRes.Value.GetObjectIds(); foreach (ObjectId tgtId in tgtIds) { DBObject tgtObj = tgtId.GetObject(OpenMode.ForRead); BlockReference tgtBlkRef; if (tgtObj is BlockReference) { tgtBlkRef = tgtObj as BlockReference; if (tgtBlkRef.BlockName == BlockAttributesBrush.orgBlkRef.BlockName)//同名块才做刷新 { foreach (ObjectId attId in tgtBlkRef.AttributeCollection) { AttributeReference attRef = attId.GetObject(OpenMode.ForWrite) as AttributeReference; if (BlockAttributesBrush.atts.ContainsKey(attRef.Tag.ToUpper()))//如果前面属性字典中含有该属性项 { if (checkedListBoxAtts.CheckedItems.Contains(attRef.Tag)) { attRef.TextString = BlockAttributesBrush.atts[attRef.Tag.ToUpper()];//如果在checkBoxList中选择了 } } attRef.DowngradeOpen();//安全起见,将打开模式降为写模式 } } } } ed.WriteMessage("本命令仅刷新同名块,请确保目标块与源块同名!"); trans.Commit(); } }
/// <summary> /// 更新块参照中的属性值 /// </summary> /// <param name="blockRefId">块参照的Id</param> /// <param name="attNameValues">需要更新的属性名称与取值</param> public static void UpdateAttributesInBlock(this ObjectId blockRefId, Dictionary <string, string> attNameValues) { //获取块参照对象 BlockReference blockRef = blockRefId.GetObject(OpenMode.ForRead) as BlockReference; if (blockRef != null) { //遍历块参照中的属性 foreach (ObjectId id in blockRef.AttributeCollection) { //获取属性 AttributeReference attref = id.GetObject(OpenMode.ForRead) as AttributeReference; //判断是否包含指定的属性名称 if (attNameValues.ContainsKey(attref.Tag.ToUpper())) { attref.UpgradeOpen();//切换属性对象为写的状态 //设置属性值 attref.TextString = attNameValues[attref.Tag.ToUpper()].ToString(); attref.DowngradeOpen();//为了安全,将属性对象的状态改为读 } } } }
public void UpdateAttributesInBlock(Database db, ObjectId blockRefId, Dictionary <string, string> attNameValues) { using (var trans = db.TransactionManager.StartTransaction()) { BlockReference blockRef = trans.GetObject(blockRefId, OpenMode.ForRead) as BlockReference; if (blockRef != null) { foreach (ObjectId id in blockRef.AttributeCollection) { AttributeReference attref = trans.GetObject(id, OpenMode.ForRead) as AttributeReference; if (attNameValues.ContainsKey(attref.Tag.ToUpper())) { attref.UpgradeOpen(); attref.TextString = attNameValues[attref.Tag.ToUpper()].ToString(); attref.DowngradeOpen(); } } } trans.Commit(); } }
//[CommandMethod("El_ExportOutCableTagList")]//20190114 public void EL_导出cableList() { //将用户坐标系转换成世界坐标系 this.acadApp.ZoomExtents(); if (ed.CurrentUserCoordinateSystem != Matrix3d.Identity) { ed.CurrentUserCoordinateSystem = Matrix3d.Identity; ed.Regen(); } const string TitleBlockName = "A3-PG"; const string reflinelayerName = "0"; List <CableTag> listCableTags = new List <CableTag>(); try { #region//读取数据 using (Transaction trans = acDB.TransactionManager.StartTransaction()) { BlockTable bt = trans.GetObject(acDB.BlockTableId, OpenMode.ForRead) as BlockTable; if (bt.Has(TitleBlockName)) { BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; foreach (ObjectId id in btr) { var ent = id.GetObject(OpenMode.ForWrite) as Entity; if (ent is BlockReference) { BlockReference blkref = ent as BlockReference; if (blkref.Name == TitleBlockName) { //获取名称 string dwgNo = ""; string AltNo = ""; var atts = blkref.AttributeCollection; if (atts != null) { foreach (ObjectId attid in atts) { AttributeReference attref = attid.GetObject(OpenMode.ForWrite) as AttributeReference; if (attref.Tag == "DWGNO") { dwgNo = attref.TextString; } if (attref.Tag == "ALT") { AltNo = attref.TextString; } attref.DowngradeOpen(); } } double scale = blkref.ScaleFactors.X; if (scale < 1) { scale = scale * 25.4; } //获取图框内的线 PromptSelectionResult psr = ed.SelectWindow(blkref.GeometricExtents.MinPoint, blkref.GeometricExtents.MaxPoint, new SelectionFilter(new TypedValue[] { new TypedValue((int)DxfCode.Start, "LWPOLYLINE"), new TypedValue((int)DxfCode.LayerName, reflinelayerName), new TypedValue((int)DxfCode.Color, 201) })); if (psr.Status == PromptStatus.OK) { foreach (SelectedObject item in psr.Value) { CableTag ct = new CableTag(item.ObjectId, acDB, scale) { DwgNo = dwgNo, Rev = "R" + AltNo }; if (ct.HasCableTagInfo) { listCableTags.Add(ct); } } } else { Circle c = new Circle(); c.Center = new Point3d((blkref.GeometricExtents.MinPoint.X + blkref.GeometricExtents.MaxPoint.X) / 2, (blkref.GeometricExtents.MinPoint.Y + blkref.GeometricExtents.MaxPoint.Y) / 2, blkref.GeometricExtents.MinPoint.Z); c.Radius = blkref.GeometricExtents.MaxPoint.X - blkref.GeometricExtents.MinPoint.X; btr.AppendEntity(c); trans.AddNewlyCreatedDBObject(c, true); c.ColorIndex = 5; } } } } } else { ed.WriteMessage("找不到块为\"A3-PG\"的块!"); } trans.Commit(); } #endregion } catch (System.Exception ex) { ed.WriteMessage(ex.Message); } if (listCableTags.Count > 0) { Excel.Application xlapp = new Excel.Application(); try { #region //导出到excek Excel.Workbook wb = xlapp.Workbooks.Add(); Excel.Worksheet ws = (wb.Sheets[1]) as Excel.Worksheet; ws.Range["A1:M1"].Value = new string[] { "DwgNo", "DwgRev", "CABLE TAG", "CABLE CODE", "CABLE LENGTH (m)", "CABLE TYPE", "CABLE OD", "SIGNAL TYPE", "FROM EQUIP.", "FROM EQUIP. LOCATION", "TO EQUIP.", "TO EQUIP. LOCATION", "Remark" }; int k = 2; foreach (var item in listCableTags) { if (item.CableTagNumbers != null) { for (int i = 0; i < item.CableTagNumbers.Length; i++) { ws.Range["A" + k].Value = item.DwgNo; ws.Range["B" + k].Value = item.Rev; ws.Range["C" + k].Value = item.CableTagNumbers[i]; ws.Range["F" + k].Value = item.CableTagSize; ws.Range["I" + k].Value = item.FromEquimentDes[0].Replace("%%U", "").Replace("%%D", " Degre e").Replace("%%C", "diameter"); ws.Range["J" + k].Value = item.FromEquimentDes[1].Replace("%%U", "").Replace("%%D", " Degre e").Replace("%%C", "diameter"); ws.Range["K" + k].Value = item.ToEquimentDes[0].Replace("%%U", "").Replace("%%D", " Degre e").Replace("%%C", "diameter"); ws.Range["L" + k].Value = item.ToEquimentDes[1].Replace("%%U", "").Replace("%%D", " Degre e").Replace("%%C", "diameter"); k++; } } } ws.Range["A1:M1"].EntireColumn.AutoFit(); ws.Range["A1:M1"].EntireColumn.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; xlapp.Visible = true; xlapp.WindowState = Excel.XlWindowState.xlMaximized; #endregion } catch (System.Exception ex) { xlapp.Quit(); ed.WriteMessage(ex.Message); } } }
public CableTag(ObjectId plineObjID, Database acDb, double TtitleBlockscale) { using (Transaction trans = acDb.TransactionManager.StartTransaction()) { var pline = trans.GetObject(plineObjID, OpenMode.ForWrite) as Polyline; var sp = pline.StartPoint; var ep = pline.EndPoint; var mp = pline.GetPoint3dAt(1); Editor ed = acadApp.DocumentManager.GetDocument(acDb).Editor; //选择文字--->起始设备 #region PromptSelectionResult psr = ed.SelectCrossingWindow(sp, new Point3d(sp.X + (4 * TtitleBlockscale), sp.Y + (10 * TtitleBlockscale), sp.Z), new SelectionFilter(new TypedValue[] { new TypedValue((int)DxfCode.Start, "Text") })); if (psr.Status == PromptStatus.OK) { List <DBText> listTxt = new List <DBText>(); foreach (SelectedObject item in psr.Value) { var txt = trans.GetObject(item.ObjectId, OpenMode.ForWrite) as DBText; txt.ColorIndex = 171; listTxt.Add(txt); } var temp = listTxt.OrderBy(c => c.Position.Y).ToList(); if (temp.Count > 0) { this.FromEquimentDes = new string[2]; this.FromEquimentDes[1] = temp[0].TextString; for (int i = temp.Count - 1; i > 0; i--) { this.FromEquimentDes[0] += temp[i].TextString + ","; } this.FromEquimentDes[0] = this.FromEquimentDes[0].Substring(0, this.FromEquimentDes[0].Length - 1); } this.HasCableTagInfo = true; } else { this.HasCableTagInfo = false; } #endregion //选择文字--->终止设备 #region psr = ed.SelectCrossingWindow(ep, new Point3d(ep.X + (4 * TtitleBlockscale), ep.Y + (10 * TtitleBlockscale), ep.Z), new SelectionFilter(new TypedValue[] { new TypedValue((int)DxfCode.Start, "Text") })); if (psr.Status == PromptStatus.OK) { List <DBText> listTxt = new List <DBText>(); foreach (SelectedObject item in psr.Value) { var txt = trans.GetObject(item.ObjectId, OpenMode.ForWrite) as DBText; txt.ColorIndex = 171; listTxt.Add(txt); } var temp = listTxt.OrderBy(c => c.Position.Y).ToList(); if (temp.Count > 0) { this.ToEquimentDes = new string[2]; this.ToEquimentDes[1] = temp[0].TextString; for (int i = temp.Count - 1; i > 0; i--) { this.ToEquimentDes[0] += temp[i].TextString + ","; } this.ToEquimentDes[0] = this.ToEquimentDes[0].Substring(0, this.ToEquimentDes[0].Length - 1); } this.HasCableTagInfo = true; } else { this.HasCableTagInfo = false; } #endregion //选择文字或图块--->电缆号码和规格 #region psr = ed.SelectCrossingWindow(mp, new Point3d(mp.X + (4 * TtitleBlockscale), mp.Y + (10 * TtitleBlockscale), mp.Z), new SelectionFilter(new TypedValue[] { new TypedValue((int)DxfCode.Start, "Text") })); if (psr.Status == PromptStatus.OK) { List <DBText> listTxt = new List <DBText>(); foreach (SelectedObject item in psr.Value) { var txt = trans.GetObject(item.ObjectId, OpenMode.ForWrite) as DBText; txt.ColorIndex = 171; listTxt.Add(txt); } var temp = listTxt.OrderBy(c => c.Position.Y).ToList(); if (temp.Count > 0) { this.CableTagSize = temp[0].TextString; this.CableTagNumbers = new string[temp.Count - 1]; for (int i = 1; i < temp.Count; i++) { this.CableTagNumbers[i - 1] += temp[i].TextString; } } this.HasCableTagInfo = true; } else { this.HasCableTagInfo = false; psr = ed.SelectCrossingWindow(mp, new Point3d(mp.X + (4 * TtitleBlockscale), mp.Y + (5 * TtitleBlockscale), mp.Z), new SelectionFilter(new TypedValue[] { new TypedValue((int)DxfCode.Start, "Insert") })); if (psr.Status == PromptStatus.OK && psr.Value.Count == 1) { List <string> listTxt = new List <string>(); var blk = trans.GetObject(psr.Value[0].ObjectId, OpenMode.ForWrite) as BlockReference; blk.ColorIndex = 171; if (blk.AttributeCollection != null) { foreach (ObjectId item in blk.AttributeCollection) { AttributeReference attref = item.GetObject(OpenMode.ForWrite) as AttributeReference; if (attref.Tag.ToUpper().Contains("CABLENUMBER") || attref.Tag.ToUpper() == "CABLETYPE") { listTxt.Add(attref.TextString); } attref.DowngradeOpen(); } var temp = listTxt.Where(c => c.Contains("(") && c.Contains(")")).ToList(); if (temp.Count > 0) { listTxt.Remove(temp[0]); this.CableTagSize = temp[0]; this.CableTagNumbers = new string[listTxt.Count]; for (int i = 0; i < listTxt.Count; i++) { this.CableTagNumbers[i] = listTxt[i]; } } } this.HasCableTagInfo = true; } else { this.HasCableTagInfo = false; } } #endregion if (this.HasCableTagInfo) { pline.ColorIndex = 171; trans.Commit(); } else { trans.Abort(); } } }
public static int UpdateAttributesInBlock( Database db, ObjectId btrId, string blockName, string attbName, string attbValue ) { // Will return the number of attributes modified int changedCount = 0; Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { BlockTableRecord btr = (BlockTableRecord)tr.GetObject( btrId, OpenMode.ForRead ); // Test each entity in the container... foreach (ObjectId entId in btr) { Entity ent = tr.GetObject(entId, OpenMode.ForRead) as Entity; if (ent != null) { BlockReference br = ent as BlockReference; if (br != null) { BlockTableRecord bd = (BlockTableRecord)tr.GetObject( br.BlockTableRecord, OpenMode.ForRead ); // ... to see whether it's a block with // the name we're after Debug.WriteLine(bd.Name); if (bd.Name.ToUpper() == blockName.ToUpper()) { // Check each of the attributes... foreach ( ObjectId arId in br.AttributeCollection ) { DBObject obj = tr.GetObject( arId, OpenMode.ForRead ); AttributeReference ar = obj as AttributeReference; if (ar != null) { Debug.WriteLine(ar.TextString); // ... to see whether it has // the tag we're after if (ar.Tag.ToUpper() == attbName.ToUpper()) { // If so, update the value // and increment the counter ar.UpgradeOpen(); ar.TextString = attbValue; ar.DowngradeOpen(); changedCount++; } } } } // Recurse for nested blocks changedCount += UpdateAttributesInBlock( db, br.BlockTableRecord, blockName, attbName, attbValue ); } } } tr.Commit(); } return(changedCount); }
private void buttonOK_Click(object sender, EventArgs e) { Dispose(); Document doc = AcadApp.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; using (Transaction trans = db.TransactionManager.StartTransaction()) { //选择要刷新的块,可以任选,挑出其中的同名快 PromptSelectionOptions tgtOpt = new PromptSelectionOptions(); tgtOpt.MessageForAdding = "选择要序列化的同名块参照"; PromptSelectionResult tgtRes = ed.GetSelection(tgtOpt); ObjectId[] tgtIds = tgtRes.Value.GetObjectIds(); List <BlockReference> tgtBlkRefs = new List <BlockReference>(); foreach (ObjectId tgtId in tgtIds) { DBObject tgtObj = tgtId.GetObject(OpenMode.ForRead); BlockReference tgtBlkRef; if (tgtObj is BlockReference) { tgtBlkRef = tgtObj as BlockReference; if (tgtBlkRef.BlockName == BlockAttributesBrush.orgBlkRef.BlockName) //同名块才做刷新 { tgtBlkRefs.Add(tgtBlkRef); //获得有效的同名块 } } } //将图块排序 var sortedTgtBlkRefs = from br in tgtBlkRefs orderby br.Position.Y descending, br.Position.X //先行后列 select br; uint numSeries = 0; foreach (BlockReference tgtBlkRef in sortedTgtBlkRefs) { foreach (ObjectId attId in tgtBlkRef.AttributeCollection) { AttributeReference attRef = attId.GetObject(OpenMode.ForWrite) as AttributeReference; if (BlockAttributesBrush.atts.ContainsKey(attRef.Tag.ToUpper())) //如果前面属性字典中含有该属性项 { if (attsForSeries.Contains(attRef.Tag)) //如果在序列化属性中 { int i = attsForSeries.IndexOf(attRef.Tag); switch (numTypeForSeries[i]) { case NumberType.数字: attRef.TextString = prefixForSeries[i] + (startNumForSeries[i] + numSeries).ToString() + suffixForSeries[i]; break; case NumberType.括号数字: attRef.TextString = prefixForSeries[i] + "(" + (startNumForSeries[i] + numSeries).ToString() + ")" + suffixForSeries[i]; break; case NumberType.汉字: attRef.TextString = prefixForSeries[i] + TextTools.IntToChChar((startNumForSeries[i] + numSeries).ToString()) + suffixForSeries[i]; break; case NumberType.括号汉字: attRef.TextString = prefixForSeries[i] + "(" + TextTools.IntToChChar((startNumForSeries[i] + numSeries).ToString()) + ")" + suffixForSeries[i]; break; } } } attRef.DowngradeOpen();//安全起见,将打开模式降为写模式 } numSeries++; } trans.Commit(); } }
public void BlkAttBrushAll() { Document doc = AcadApp.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; using (Transaction trans = db.TransactionManager.StartTransaction()) { //提示用户选择数据源块参照 PromptEntityOptions orgOpt = new PromptEntityOptions("选择源格式块"); orgOpt.SetRejectMessage("选择的不是块!"); orgOpt.AddAllowedClass(typeof(BlockReference), true); //只能选择块参照 PromptEntityResult orgRes = ed.GetEntity(orgOpt); if (orgRes.Status == PromptStatus.OK) //选择正确 { orgBlkRef = orgRes.ObjectId.GetObject(OpenMode.ForRead) as BlockReference; if (orgBlkRef.AttributeCollection.Count == 0) { ed.WriteMessage("所选对象不包含属性!"); return; } else { atts = new Dictionary <string, string>(); //初始化属性集字典 foreach (ObjectId attId in orgBlkRef.AttributeCollection) //获得源块属性值 { AttributeReference attRef = attId.GetObject(OpenMode.ForRead) as AttributeReference; atts.Add(attRef.Tag.ToUpper(), attRef.TextString); } //选择要刷新的块,可以任选,挑出其中的同名快 PromptSelectionOptions tgtOpt = new PromptSelectionOptions(); tgtOpt.MessageForAdding = "选择要刷新的块参照"; PromptSelectionResult tgtRes = ed.GetSelection(tgtOpt); ObjectId[] tgtIds = tgtRes.Value.GetObjectIds(); foreach (ObjectId tgtId in tgtIds) { DBObject tgtObj = tgtId.GetObject(OpenMode.ForRead); BlockReference tgtBlkRef; if (tgtObj is BlockReference) { tgtBlkRef = tgtObj as BlockReference; if (tgtBlkRef.BlockName == orgBlkRef.BlockName)//同名块才做刷新 { foreach (ObjectId attId in tgtBlkRef.AttributeCollection) { AttributeReference attRef = attId.GetObject(OpenMode.ForWrite) as AttributeReference; if (atts.ContainsKey(attRef.Tag.ToUpper()))//如果前面属性字典中含有该属性项 { attRef.TextString = atts[attRef.Tag.ToUpper()]; } attRef.DowngradeOpen();//安全起见,将打开模式降为写模式 } } } } } } ed.WriteMessage("本命令仅刷新同名块,请确保目标块与源块同名!"); trans.Commit(); } }
private void click_btn_AddPart(object sender, EventArgs e) { DocumentLock loc = doc.LockDocument(); using (loc) { drawingToAdd.Replace(@"/", @"\"); string drawingFileName = drawingToAdd.Substring(drawingToAdd.IndexOf(@"/", (drawingToAdd.IndexOf(@"/") + 1)) + 1, drawingToAdd.Length - drawingToAdd.IndexOf(@"/", (drawingToAdd.IndexOf(@"/") + 1)) - 1); Database tmpDb = new Database(false, true); tmpDb.ReadDwgFile(@"\\thehaskellco.net\Remote\AtlantaData\_Disciplines\Controls\4 Controls Programming\DVBs and DLLs\Footprints\Panel\" + drawingToAdd + ".dwg", System.IO.FileShare.Read, true, ""); using (Transaction tr = db.TransactionManager.StartTransaction()) { bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); if (!bt.Has(drawingFileName)) { db.Insert(drawingFileName, tmpDb, true); } tr.Commit(); } using (Transaction tr = db.TransactionManager.StartTransaction()) { bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); btr = (BlockTableRecord)bt[drawingFileName].GetObject(OpenMode.ForRead); bool tagFound = false; if (btr != null) { foreach (ObjectId objId in btr) { AttributeDefinition attDef = tr.GetObject(objId, OpenMode.ForRead) as AttributeDefinition; if (attDef != null) { if (attDef.Tag.ToUpper() == "TABLE_PART_NUM") { tagFound = true; } else if (attDef.Tag.ToUpper() == "TABLE_DESCRIPTION") { tagFound = true; } } } try { if (!tagFound) { AttributeDefinition acAttDef = new AttributeDefinition(); acAttDef.Position = new Point3d(0, 0, 0); acAttDef.Prompt = "Part Number?"; acAttDef.Tag = "TABLE_PART_NUM"; acAttDef.Invisible = true; acAttDef.Height = 1; acAttDef.Justify = AttachmentPoint.MiddleCenter; AttributeDefinition acAttDef2 = new AttributeDefinition(); acAttDef2.Position = new Point3d(0, 0, 0); acAttDef2.Prompt = "Description?"; acAttDef2.Tag = "TABLE_DESCRIPTION"; acAttDef2.Invisible = true; acAttDef2.Height = 1; acAttDef2.Justify = AttachmentPoint.MiddleCenter; AttributeDefinition acAttDef3 = new AttributeDefinition(); acAttDef3.Position = new Point3d(0, -10, 0); acAttDef3.Prompt = "DO NOT CHANGE"; acAttDef3.Tag = "AUTO_UPDATE"; acAttDef3.TextString = "1"; acAttDef3.Invisible = true; acAttDef3.Height = 1; acAttDef3.Justify = AttachmentPoint.MiddleCenter; btr.UpgradeOpen(); btr.AppendEntity(acAttDef); btr.AppendEntity(acAttDef2); btr.AppendEntity(acAttDef3); tr.AddNewlyCreatedDBObject(acAttDef, true); tr.AddNewlyCreatedDBObject(acAttDef2, true); tr.AddNewlyCreatedDBObject(acAttDef3, true); btr.DowngradeOpen(); tr.Commit(); } } catch { ed.WriteMessage("Could not add attributes to block."); } } } using (Transaction tr = db.TransactionManager.StartTransaction()) { if (btr != null) { MyBlockJig blockJig = new MyBlockJig(); Point3d point; PromptResult res = blockJig.DragMe(btr.ObjectId, out point); if (res.Status == PromptStatus.OK) { BlockTableRecord curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); BlockReference insert = new BlockReference(point, btr.ObjectId); curSpace.AppendEntity(insert); tr.AddNewlyCreatedDBObject(insert, true); foreach (ObjectId id in btr) { DBObject obj = id.GetObject(OpenMode.ForWrite); AttributeDefinition attDef = obj as AttributeDefinition; if ((attDef != null) && (!attDef.Constant)) { using (AttributeReference attRef = new AttributeReference()) { attRef.SetAttributeFromBlock(attDef, insert.BlockTransform); insert.AttributeCollection.AppendAttribute(attRef); tr.AddNewlyCreatedDBObject(attRef, true); } } } } } tr.Commit(); doc.SendStringToExecute("regenall ", true, false, false); } using (Transaction tr = db.TransactionManager.StartTransaction()) { ObjectId msId = bt[BlockTableRecord.ModelSpace]; BlockTableRecord btr = (BlockTableRecord)tr.GetObject(msId, OpenMode.ForRead); foreach (ObjectId entId in btr) { Entity ent = (Entity)tr.GetObject(entId, OpenMode.ForWrite); if (ent != null) { BlockReference br = ent as BlockReference; if (br != null) { BlockTableRecord bd = (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead); if (bd.Name.ToUpper() == drawingFileName.ToUpper()) { foreach (ObjectId arId in br.AttributeCollection) { DBObject obj = tr.GetObject(arId, OpenMode.ForWrite); AttributeReference ar = obj as AttributeReference; if (ar != null) { if (ar.Tag == "AUTO_UPDATE") { if (ar.TextString == "1") { foreach (ObjectId attRefId in br.AttributeCollection) { DBObject objRef = tr.GetObject(attRefId, OpenMode.ForWrite); AttributeReference aRef = objRef as AttributeReference; if (aRef.Tag == "TABLE_PART_NUM") { aRef.UpgradeOpen(); aRef.TextString = partNumberToAdd; aRef.DowngradeOpen(); } else if (aRef.Tag == "TABLE_DESCRIPTION") { aRef.UpgradeOpen(); aRef.TextString = descriptionToAdd; aRef.DowngradeOpen(); } } } ar.TextString = "0"; } } } } } } } tr.Commit(); } } }
public string CHECKATTRIBUTEVALUE( string blockName, string attbName ) { Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; doc.LockDocument(); string attributeValue = ""; // Get the IDs of the spaces we want to process // and simply call a function to process each ObjectId psId; Transaction tr1 = db.TransactionManager.StartTransaction(); using (tr1) { BlockTable bt = (BlockTable)tr1.GetObject( db.BlockTableId, OpenMode.ForRead ); psId = bt[BlockTableRecord.PaperSpace]; // Not needed, but quicker than aborting tr1.Commit(); } // Will return the value of the named attribute. Transaction tr2 = doc.TransactionManager.StartTransaction(); using (tr2) { BlockTableRecord btr = (BlockTableRecord)tr2.GetObject( psId, OpenMode.ForRead ); // Test each entity in the container... foreach (ObjectId entId in btr) { Entity ent = tr2.GetObject(entId, OpenMode.ForRead) as Entity; if (ent != null) { //////////attributeValue = "1"; BlockReference br = ent as BlockReference; if (br != null) { ////////////attributeValue = "2"; BlockTableRecord bd = (BlockTableRecord)tr2.GetObject( br.BlockTableRecord, OpenMode.ForRead ); // ... to see whether it's a block with // the name we're after ////////////attributeValue = bd.Name.ToUpper() + " = " + blockName; if (bd.Name.ToUpper() == blockName) { // Check each of the attributes... foreach (ObjectId arId in br.AttributeCollection) { DBObject obj = tr2.GetObject(arId, OpenMode.ForRead); AttributeReference ar = obj as AttributeReference; if (ar != null) { //attributeValue = ar.Tag.ToUpper() + " = " + attbName; // ... to see whether it has // the tag we're after if (ar.Tag.ToUpper() == attbName) { ////////////attributeValue = "5"; // If so, update the value // and increment the counter ar.UpgradeOpen(); attributeValue = ar.TextString; ar.DowngradeOpen(); } } } } } } } tr2.Commit(); } return(attributeValue); }