//[CommandMethod("BJ")] public static void BlockJigCmd(Double finalCalcText) { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; //Set the block Name to use. string pr = "TagHgr"; Transaction tr = doc.TransactionManager.StartTransaction(); using (tr) { BlockTable bt = (BlockTable)tr.GetObject( db.BlockTableId, OpenMode.ForRead ); if (!bt.Has(pr)) { ed.WriteMessage( "\nBlock \"" + pr + "\" not found."); return; } BlockTableRecord space = (BlockTableRecord)tr.GetObject( db.CurrentSpaceId, OpenMode.ForWrite ); BlockTableRecord btr = (BlockTableRecord)tr.GetObject( bt[pr], OpenMode.ForRead); // Block needs to be inserted to current space before // being able to append attribute to it BlockReference br = new BlockReference(new Point3d(), btr.ObjectId); space.AppendEntity(br); tr.AddNewlyCreatedDBObject(br, true); Dictionary<ObjectId, AttInfo> attInfo = new Dictionary<ObjectId, AttInfo>(); if (btr.HasAttributeDefinitions) { foreach (ObjectId id in btr) { DBObject obj = tr.GetObject(id, OpenMode.ForRead); AttributeDefinition ad = obj as AttributeDefinition; if (ad != null && !ad.Constant) { AttributeReference ar = new AttributeReference(); ar.SetAttributeFromBlock(ad, br.BlockTransform); ar.Position = ad.Position.TransformBy(br.BlockTransform); if (ad.Justify != AttachmentPoint.BaseLeft) { ar.AlignmentPoint = ad.AlignmentPoint.TransformBy(br.BlockTransform); } if (ar.IsMTextAttribute) { ar.UpdateMTextAttribute(); } //ar.TextString = ad.TextString; ar.TextString = PopulateAttributes(ad.Tag, finalCalcText.ToString()); ObjectId arId = br.AttributeCollection.AppendAttribute(ar); tr.AddNewlyCreatedDBObject(ar, true); // Initialize our dictionary with the ObjectId of // the attribute reference + attribute definition info attInfo.Add( arId, new AttInfo( ad.Position, ad.AlignmentPoint, ad.Justify != AttachmentPoint.BaseLeft ) ); } } } // Run the jig BlockJig myJig = new BlockJig(tr, br, attInfo); if (myJig.Run() != PromptStatus.OK) return; // Commit changes if user accepted, otherwise discard tr.Commit(); } }
/** * 对话框隐藏后无法对焦点的问题通过设置dialog的parent可以解决 * 这里是重点 * 找到父窗口 **/ private void BlockImage_MouseDoubleClick(object sender, MouseEventArgs e) { //string Prefix = "_"; if (e.Button == MouseButtons.Right) { return; } Form Parent = this.FindForm(); Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor; DWGLib.Dialog.LibaryDialog Dialog = Parent as DWGLib.Dialog.LibaryDialog; Document Doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; ed.WriteMessage("\n", true, true, false); if (this._type == 1) { Dialog.Hide(); if (File.Exists(this.FilePath)) { Document _Doc = AcadApp.DocumentManager.Open(this.FilePath, false); } else { AcadApp.ShowAlertDialog(string.Format("文件{0}不存在", this.FilePath)); } Dialog.Close(); Dialog.Dispose(); return; } if (Dialog != null) { ObjectId BlockId = default(ObjectId); Dialog.Hide(); Database TargetDB = HostApplicationServices.WorkingDatabase; DocumentLock DocLock = Doc.LockDocument(); string BlockName = Path.GetFileNameWithoutExtension(this.FilePath); WBlockBetweenDataBase(BlockName); using (Transaction Tr = TargetDB.TransactionManager.StartTransaction()) { BlockTable Btr = Tr.GetObject(TargetDB.BlockTableId, OpenMode.ForWrite) as BlockTable; BlockTableRecord ModelSpace = (BlockTableRecord)SymbolUtilityServices.GetBlockModelSpaceId(TargetDB).GetObject(OpenMode.ForWrite); try { BlockId = Btr[BlockName]; }catch (Autodesk.AutoCAD.Runtime.Exception ex) { //这样可以吗; Dialog.Show(); AcadApp.ShowAlertDialog(ex.Message); DocLock.Dispose(); TargetDB.Dispose(); return; } #region ////<summary> /***** * if (Stats == ErrorStatus.OK) * { * BlockId = Btr[BlockName]; * } * /**** * try * { * if (Btr.Has(BlockName)) * { * BlockId = Btr[BlockName]; * }else * { * BlockId = TargetDB.Insert(BlockName, SourceDB, true); * * } * }catch(Autodesk.AutoCAD.Runtime.Exception ex) * { * AcadApp.ShowAlertDialog("插入图块失败"); * * Tr.Commit(); * DocLock.Dispose(); * TargetDB.Dispose(); * SourceDB.Dispose(); * return; * } * } * else * { * AcadApp.ShowAlertDialog("打开文件失败"); * Dialog.Close(); * * Tr.Commit(); * DocLock.Dispose(); * TargetDB.Dispose(); * SourceDB.Dispose(); * return; * } ****/ #endregion BlockReference Block = new BlockReference(new Autodesk.AutoCAD.Geometry.Point3d(0, 0, 0), BlockId); Block.Highlight(); BlockJig _BlockJig = new BlockJig(Block); bool IsOK = _BlockJig.RunBlockJig(Block); //这里的运行情况是这样的,do......while,不会按照计算机的时钟频率运行,而是点击了屏幕之后才会运行,每点击一次运行一次 //这个AutoCAD运行的特殊的地方。 #region #endregion if (!IsOK) { AcadApp.UpdateScreen(); Dialog.Show(); ed.WriteMessage("取消插入\n", true, true, false); Tr.Commit(); DocLock.Dispose(); TargetDB.Dispose(); } else { Dialog.Close(); ed.WriteMessage("\n插入成功\n", true, true, false); DBObjectCollection Objs = new DBObjectCollection(); ModelSpace.AppendEntity(Block); Tr.AddNewlyCreatedDBObject(Block, true); // Block.ExplodeToOwnerSpace(); Tr.Commit(); DocLock.Dispose(); TargetDB.Dispose(); } //if is not OK } } }
} //end ImportBlocks() //[CommandMethod("BJ")] static public void BlockJigCmd(Double finalCalcText) { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; //Set the block Name to use. string pr = "TagHgr"; Transaction tr = doc.TransactionManager.StartTransaction(); using (tr) { BlockTable bt = (BlockTable)tr.GetObject( db.BlockTableId, OpenMode.ForRead ); if (!bt.Has(pr)) { ed.WriteMessage( "\nBlock \"" + pr + "\" not found."); return; } BlockTableRecord space = (BlockTableRecord)tr.GetObject( db.CurrentSpaceId, OpenMode.ForWrite ); BlockTableRecord btr = (BlockTableRecord)tr.GetObject( bt[pr], OpenMode.ForRead); // Block needs to be inserted to current space before // being able to append attribute to it BlockReference br = new BlockReference(new Point3d(), btr.ObjectId); space.AppendEntity(br); tr.AddNewlyCreatedDBObject(br, true); Dictionary <ObjectId, AttInfo> attInfo = new Dictionary <ObjectId, AttInfo>(); if (btr.HasAttributeDefinitions) { foreach (ObjectId id in btr) { DBObject obj = tr.GetObject(id, OpenMode.ForRead); AttributeDefinition ad = obj as AttributeDefinition; if (ad != null && !ad.Constant) { AttributeReference ar = new AttributeReference(); ar.SetAttributeFromBlock(ad, br.BlockTransform); ar.Position = ad.Position.TransformBy(br.BlockTransform); if (ad.Justify != AttachmentPoint.BaseLeft) { ar.AlignmentPoint = ad.AlignmentPoint.TransformBy(br.BlockTransform); } if (ar.IsMTextAttribute) { ar.UpdateMTextAttribute(); } //ar.TextString = ad.TextString; ar.TextString = PopulateAttributes(ad.Tag, finalCalcText.ToString()); ObjectId arId = br.AttributeCollection.AppendAttribute(ar); tr.AddNewlyCreatedDBObject(ar, true); // Initialize our dictionary with the ObjectId of // the attribute reference + attribute definition info attInfo.Add( arId, new AttInfo( ad.Position, ad.AlignmentPoint, ad.Justify != AttachmentPoint.BaseLeft ) ); } } } // Run the jig BlockJig myJig = new BlockJig(tr, br, attInfo); if (myJig.Run() != PromptStatus.OK) { return; } // Commit changes if user accepted, otherwise discard tr.Commit(); } }
private void AddZxbz() { var ssdw = ComBox_SSDW.Text; var bzlx = ComBox_BZLX.Text; var bzText = TXT_BZNR.Text.Trim(); if (string.IsNullOrEmpty(bzlx)) { MessageBox.Show("标注类型不能为空!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (!string.IsNullOrEmpty(bzText)) { //if (!DBEntityFinder.Instance.HasZxbz(entityArgs.g3eObject.G3E_FID.ToString())) //{ isInsert = true; Btn_Add.Enabled = false; //杂项标注图层 var layerID = DCadApi.addLayer("杂项标注"); //标注大小 var lbHeight = 0.35 / MapConfig.Instance.earthscale; //标注颜色 var color = Autodesk.AutoCAD.Colors.Color.FromRgb(0, 0, 0); ViewHelper.zxLabelManagerPs.KeepFocus = false; Entity mText; if (bzText.Contains("\n")) { mText = SymbolLabel.Mtext(bzText, Point3d.Origin, 0, lbHeight, layerID); //if (!PublicMethod.Instance.MultTextStyleId.IsNull) // ((MText)mText).TextStyleId = PublicMethod.Instance.MultTextStyleId; } else { mText = SymbolLabel.AddText(bzText, Point3d.Origin, 0, lbHeight, layerID, color); } if (BlockJig.Jig(mText, true)) { isInsert = false; DCadApi.AddPinchPoint(mText); DCadApi.AddLabelPinchPoint(mText); //添加到当前模型中 var objId = PublicMethod.Instance.ToModelSpace(mText); //转换成空间坐标 var mpt = ConvertGeometry.Instance.GetMultipoint(objId); ElectronSymbol pt = null; //添加杂标数据 var result = InsertDBEntity.InsertZXBZ(entityArgs.g3eObject.G3E_FID, bzText, bzlx, ssdw, mpt, ref pt); if (pt != null) { //保存记录 DBSymbolFinder.Instance.Add(objId, pt); } var message = result ? "新增成功!" : "新增失败!"; ViewHelper.zxLabelManagerPs.KeepFocus = true; PublicMethod.Instance.ShowMessage(message); } Btn_Add.Enabled = true; //} //else // MessageBox.Show("杂项标注已添加!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Stop); } else { TXT_BZNR.Focus(); MessageBox.Show("标注内容不能为空!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Stop); } }