public void updateTBFromFile() { Database db = HostApplicationServices.WorkingDatabase; Editor ed = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument.Editor; //read external json PromptFileNameResult pfnr = ed.GetFileNameForOpen("\nSpecify json file"); if (pfnr.Status != PromptStatus.OK) { ed.WriteMessage("\nerror to read external json file " + pfnr.Status.ToString()); return; } string paramFile = pfnr.StringResult; string rawJsonContent = File.ReadAllText(paramFile); Transaction tr = db.TransactionManager.StartTransaction(); // Start the transaction try { //convert json to json class jsonDrawing jsonDrawingInstance = JsonConvert.DeserializeObject <jsonDrawing>(rawJsonContent); if (jsonDrawingInstance == null) { ed.WriteMessage("\nraw json file is bad!\n"); return; } //string tblayer = jsonDrawingInstance.tblayer; //create the layer for signature ObjectId lyId = createLayer("signature_layer"); if (lyId.IsNull) { ed.WriteMessage("\nlayer cannot be created"); return; } // Build a filter list so that only block references are selected TypedValue[] filList = new TypedValue[1] { new TypedValue((int)DxfCode.Start, "INSERT") }; SelectionFilter filter = new SelectionFilter(filList); PromptSelectionResult res = ed.SelectAll(filter); // Do nothing if selection is unsuccessful if (res.Status != PromptStatus.OK) { ed.WriteMessage("\nno any Insert in this drawing!"); return; } SelectionSet selSet = res.Value; ObjectId[] idArray = selSet.GetObjectIds(); foreach (ObjectId blkId in idArray) { BlockReference blkRef = (BlockReference)tr.GetObject(blkId, OpenMode.ForRead); BlockTableRecord btr = (BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead); ed.WriteMessage("\nBlock: " + btr.Name); btr.Dispose(); AttributeCollection attCol = blkRef.AttributeCollection; foreach (ObjectId attId in attCol) { AttributeReference attRef = (AttributeReference)tr.GetObject(attId, OpenMode.ForWrite); //check the block ref of title block on the specific layer if (attRef.Layer == Helper.tblayer) { foreach (var eachAtt in jsonDrawingInstance.tbjson) { if (eachAtt.tag == attRef.Tag) { ed.WriteMessage("\nbegin: " + eachAtt.tag + " \n"); ed.WriteMessage("\nis image: " + eachAtt.isImage + " \n"); if (eachAtt.isImage) { attRef.TextString = ""; //create image for signature createImages(eachAtt, tr, lyId); } else { attRef.TextString = eachAtt.content; } break; ed.WriteMessage("\nend: " + eachAtt.tag + " \n"); } } } } } tr.Commit(); } catch (Autodesk.AutoCAD.Runtime.Exception ex) { ed.WriteMessage(("\nException: " + ex.Message)); } finally { tr.Dispose(); } db.SaveAs("newTB.dwg", DwgVersion.Newest); }
/// <summary> /// /// </summary> /// <param name="isLocalTest">true: simulate an signature image to a json string. the example image is testsig.png</param> void GenerateTBJson(bool isLocalTest = false) { Editor ed = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument.Editor; Database db = HostApplicationServices.WorkingDatabase; Transaction tr = db.TransactionManager.StartTransaction(); try { ed.WriteMessage("\nbegin att checking"); // Build a filter list so that only block references are selected TypedValue[] filList = new TypedValue[1] { new TypedValue((int)DxfCode.Start, "INSERT") }; SelectionFilter filter = new SelectionFilter(filList); PromptSelectionResult res = ed.SelectAll(filter); // Do nothing if selection is unsuccessful if (res.Status != PromptStatus.OK) { return; } SelectionSet selSet = res.Value; ObjectId[] idArray = selSet.GetObjectIds(); List <jsonAttribute> jsonDataArray = new List <jsonAttribute>(); ed.WriteMessage("\nbegin each checking"); foreach (ObjectId blkId in idArray) { BlockReference blkRef = (BlockReference)tr.GetObject(blkId, OpenMode.ForRead); BlockTableRecord btr = (BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead); ed.WriteMessage("\nBlock: " + btr.Name); btr.Dispose(); AttributeCollection attCol = blkRef.AttributeCollection; //attribute index int index = 0; foreach (ObjectId attId in attCol) { AttributeReference attRef = (AttributeReference)tr.GetObject(attId, OpenMode.ForRead); if (attRef.Layer == "AM_BOR") { ed.WriteMessage("\nbegin: " + attRef.Tag); string _height = attRef.Height.ToString(); string _tag = attRef.Tag; int pFrom = _tag.IndexOf("{") + 1; int pTo = _tag.LastIndexOf("}"); string _width_radio = _tag.Substring(pFrom, pTo - pFrom); string _pos = attRef.Position.X.ToString() + "," + attRef.Position.Y.ToString() + "," + attRef.Position.Z.ToString(); string _cont = attRef.TextString; var jsonData = new jsonAttribute() { tag = _tag, height = _height, width_ratio = _width_radio, position = _pos, content = _cont, isImage = false, imgbase64 = "" }; //if this is to test an image locally //only simulate the attribute in even index will be an image if (isLocalTest && (index % 2 == 1)) { jsonData.isImage = true; jsonData.imgbase64 = imageToBase64(); } jsonDataArray.Add(jsonData); ed.WriteMessage("end: " + attRef.Tag); index++; } //tag: {12.3} is the\ ratio of the defined width for the text to the text height. For example, if the text height is 5 units, and the width of the available space is 100 units, the value between the curly brackets is 20. //http://knowledge.autodesk.com/support/autocad-mechanical/getting-started/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-Mechanical/files/GUID-ADFE83F7-CE92-4996-8231-D3C5FD5A1A92-htm.html } } tr.Commit(); var jsonDrawingInstance = new jsonDrawing() { tbjson = jsonDataArray.ToArray <jsonAttribute>() }; // this is the Newtonsoft API method string json_data = JsonConvert.SerializeObject(jsonDrawingInstance); var jsonOut = Path.Combine(Helper.jsonfilename); FileStream fs = new FileStream(jsonOut, FileMode.Create); StreamWriter sw = new StreamWriter(fs); try { sw.Write(json_data); sw.Flush(); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message.ToString()); } finally { sw.Close(); fs.Close(); } ed.WriteMessage("\nend att checking"); } catch (Autodesk.AutoCAD.Runtime.Exception ex) { ed.WriteMessage(("\nException: " + ex.Message)); return; } finally { tr.Dispose(); } }
/// <summary> /// /// </summary> /// <param name="isLocalTest">true: simulate an signature image to a json string. the example image is testsig.png</param> void GenerateTBJson(bool isLocalTest = false) { Editor ed = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument.Editor; Database db = HostApplicationServices.WorkingDatabase; Transaction tr = db.TransactionManager.StartTransaction(); try { ed.WriteMessage("\nbegin att checking"); // Build a filter list so that only block references are selected TypedValue[] filList = new TypedValue[1] { new TypedValue((int)DxfCode.Start, "INSERT") }; SelectionFilter filter = new SelectionFilter(filList); PromptSelectionResult res = ed.SelectAll(filter); // Do nothing if selection is unsuccessful if (res.Status != PromptStatus.OK) return; SelectionSet selSet = res.Value; ObjectId[] idArray = selSet.GetObjectIds(); List<jsonAttribute> jsonDataArray = new List<jsonAttribute>(); ed.WriteMessage("\nbegin each checking"); foreach (ObjectId blkId in idArray) { BlockReference blkRef = (BlockReference)tr.GetObject(blkId, OpenMode.ForRead); BlockTableRecord btr = (BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead); ed.WriteMessage("\nBlock: " + btr.Name); btr.Dispose(); AttributeCollection attCol = blkRef.AttributeCollection; //attribute index int index = 0; foreach (ObjectId attId in attCol) { AttributeReference attRef = (AttributeReference)tr.GetObject(attId, OpenMode.ForRead); if (attRef.Layer == "AM_BOR") { ed.WriteMessage("\nbegin: " + attRef.Tag); string _height = attRef.Height.ToString(); string _tag = attRef.Tag; int pFrom = _tag.IndexOf("{") + 1; int pTo = _tag.LastIndexOf("}"); string _width_radio = _tag.Substring(pFrom, pTo - pFrom); string _pos = attRef.Position.X.ToString() + "," + attRef.Position.Y.ToString() + "," + attRef.Position.Z.ToString(); string _cont = attRef.TextString; var jsonData = new jsonAttribute() { tag = _tag, height = _height, width_ratio = _width_radio, position = _pos, content = _cont, isImage = false, imgbase64 = "" }; //if this is to test an image locally //only simulate the attribute in even index will be an image if (isLocalTest && (index%2==1)) { jsonData.isImage = true; jsonData.imgbase64 = imageToBase64(); } jsonDataArray.Add(jsonData); ed.WriteMessage("end: " + attRef.Tag); index++; } //tag: {12.3} is the\ ratio of the defined width for the text to the text height. For example, if the text height is 5 units, and the width of the available space is 100 units, the value between the curly brackets is 20. //http://knowledge.autodesk.com/support/autocad-mechanical/getting-started/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-Mechanical/files/GUID-ADFE83F7-CE92-4996-8231-D3C5FD5A1A92-htm.html } } tr.Commit(); var jsonDrawingInstance = new jsonDrawing() { tbjson = jsonDataArray.ToArray<jsonAttribute>() }; // this is the Newtonsoft API method string json_data = JsonConvert.SerializeObject(jsonDrawingInstance); var jsonOut = Path.Combine(Helper.jsonfilename); FileStream fs = new FileStream(jsonOut, FileMode.Create); StreamWriter sw = new StreamWriter(fs); try { sw.Write(json_data); sw.Flush(); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message.ToString()); } finally { sw.Close(); fs.Close(); } ed.WriteMessage("\nend att checking"); } catch (Autodesk.AutoCAD.Runtime.Exception ex) { ed.WriteMessage(("\nException: " + ex.Message)); return; } finally { tr.Dispose(); } }