工具箱相关
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

LSTKAllCls.cs 111KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using Autodesk.AutoCAD.Geometry;
  7. using Autodesk.AutoCAD.DatabaseServices;
  8. using Autodesk.AutoCAD.ApplicationServices;
  9. using Autodesk.AutoCAD.EditorInput;
  10. using System.IO;
  11. namespace T_cad
  12. {
  13. class LSTKAllCls
  14. {
  15. public LSTKAllCls()
  16. {
  17. }
  18. /// <summary>
  19. /// 测试函数
  20. /// </summary>
  21. public void Function()
  22. {
  23. #region//后台打开一个dwg文件,最后用DataBase.saveas()进行保存
  24. try
  25. {
  26. //for (int i = 0; i < 10; i++)
  27. //{
  28. var filenamemuban = @"D:\test\1000tk.dwg";
  29. Database myDB = new Database(false, true);
  30. myDB.ReadDwgFile(filenamemuban, System.IO.FileShare.ReadWrite, false, "");
  31. ////选中所有对象进行平移
  32. using (var myTran = myDB.TransactionManager.StartTransaction())
  33. {
  34. var bt = (BlockTable)(myTran.GetObject(myDB.BlockTableId, OpenMode.ForWrite));
  35. foreach (ObjectId blid in bt)
  36. {
  37. var btr = (BlockTableRecord)(myTran.GetObject(blid, OpenMode.ForWrite));
  38. BlockTableRecordEnumerator btrenu = btr.GetEnumerator();
  39. while (btrenu.MoveNext())
  40. {
  41. ObjectId myobjid = btrenu.Current;
  42. Entity myentity = myTran.GetObject(myobjid, OpenMode.ForWrite) as Entity;
  43. //测试后台可否执行的代码
  44. //myentity.Color = Autodesk.AutoCAD.Colors.Color.FromColor(System.Drawing.Color.Blue);
  45. myentity.ColorIndex = 5;
  46. Point3d basept = new Point3d(0, 0, 0);
  47. Point3d targetpt = new Point3d(3000, 3000, 0);
  48. Vector3d vec = targetpt - basept;
  49. Matrix3d mt3d = Matrix3d.Displacement(vec);
  50. myentity.TransformBy(mt3d);
  51. // //平移代码
  52. // /// <summary>
  53. ///// 指定基点与目标移动实体
  54. ///// </summary>
  55. ///// <param name="ent">实体移动</param>
  56. ///// <param name="BasePt">基点</param>
  57. ///// <param name="TargetPt">目标点</param>
  58. //public static void Move(Entity ent, Point3d basePt, Point3d targetPt)
  59. //{
  60. //Vector3d vec = targetPt - basePt;
  61. //Matrix3d mt = Matrix3d.Displacement(vec);
  62. //ent.TransformBy(mt);
  63. //}
  64. }
  65. btrenu.Dispose();
  66. btr.Dispose();
  67. }
  68. bt.Dispose();
  69. myTran.Commit();
  70. myTran.Dispose();
  71. }
  72. var resultfilename = @"D:\test\result\1.dwg";
  73. //var resultfilename = @"D:\test\result\1000tk" + i.ToString() + ".dwg";
  74. myDB.SaveAs(resultfilename, DwgVersion.Current);
  75. myDB.CloseInput(true);
  76. myDB.Dispose();
  77. //}//end for
  78. }
  79. catch (Exception ex)
  80. {
  81. MessageBox.Show(ex.ToString());
  82. }
  83. #endregion/////////////
  84. }
  85. public void mianfunction()
  86. {
  87. FolderBrowserDialog fbd = new FolderBrowserDialog();
  88. fbd.Description = "请设置DOM路径";
  89. if (fbd.ShowDialog() == DialogResult.OK)
  90. {
  91. string pathdirectory = fbd.SelectedPath;
  92. DirectoryInfo di = new DirectoryInfo(pathdirectory);
  93. FileInfo[] fiarray = di.GetFiles();
  94. foreach (FileInfo fi in fiarray)
  95. {
  96. string filename = fi.FullName;
  97. if (filename.Contains(".tfw"))
  98. {
  99. DOMAndDWGfunction(fi);
  100. }
  101. }
  102. }
  103. MessageBox.Show("程序运行结束");
  104. }
  105. public void DOMAndDWGfunction(FileInfo fi)
  106. {
  107. string filename = fi.FullName;
  108. //读取tfwcode
  109. string tfwfilename = filename;
  110. string imageName = filename.Replace(".tfw", ".tif");
  111. string dwgfilename = filename.Replace(".tfw", ".dwg");
  112. string myName = fi.Name.Replace(".tfw", "");//图幅号名
  113. double scale = 0;
  114. double X = 0, Y = 0;
  115. try
  116. {
  117. StreamReader sr = new StreamReader(tfwfilename);
  118. scale = Convert.ToDouble(sr.ReadLine());
  119. sr.ReadLine();
  120. sr.ReadLine();
  121. sr.ReadLine();
  122. X = Convert.ToDouble(sr.ReadLine());
  123. Y = Convert.ToDouble(sr.ReadLine());
  124. sr.Close();
  125. sr.Dispose();
  126. }
  127. catch (Exception ex)
  128. {
  129. MessageBox.Show(ex.ToString ());
  130. }
  131. //移动dwg图框code
  132. //var filenamemuban = @"E:\2015Work\20150309DOM套图库程序\moban\1000lstk.dwg";
  133. var filenamemuban = @"E:\2015Work\20150309DOM套图库程序\moban\1000lstkcode.dwg";
  134. Database myDB = new Database(false, true);
  135. myDB.ReadDwgFile(filenamemuban, System.IO.FileShare.ReadWrite, false, "");
  136. myDB.CloseInput(true);
  137. //左上角坐标除去外扩
  138. double xmove = X + 10;
  139. double ymove = Y - 10;
  140. //左上 left top
  141. double xlt = xmove/1000.0;
  142. double ylt = ymove / 1000.0;
  143. string xltstr = xlt.ToString("f1");
  144. string yltstr = ylt.ToString("f1");
  145. //右上 right top
  146. double xrt = (xmove + 500) / 1000.0;
  147. double yrt = ymove / 1000.0;
  148. string xrtstr = xrt.ToString("f1");
  149. string yrtstr = yrt.ToString("f1");
  150. //左下 left bottom
  151. double xlb = xmove / 1000.0;
  152. double ylb = (ymove - 500) / 1000.0;
  153. string xlbstr = xlb.ToString("f1");
  154. string ylbstr = ylb.ToString("f1");
  155. //右下 right bottom
  156. double xrb = (xmove + 500) / 1000.0;
  157. double yrb = (ymove - 500) / 1000.0;
  158. string xrbstr = xrb.ToString("f1");
  159. string yrbstr = yrb.ToString("f1");
  160. ////X = 559000.000 Y = 3349500.000 Z = 2874.000
  161. ////选中所有对象进行平移
  162. using (var myTran = myDB.TransactionManager.StartTransaction())
  163. {
  164. try
  165. {
  166. var bt = (BlockTable)(myTran.GetObject(myDB.BlockTableId, OpenMode.ForWrite));
  167. foreach (ObjectId blid in bt)
  168. {
  169. var btr = (BlockTableRecord)(myTran.GetObject(blid, OpenMode.ForWrite));
  170. BlockTableRecordEnumerator btrenu = btr.GetEnumerator();
  171. while (btrenu.MoveNext())
  172. {
  173. ObjectId myobjid = btrenu.Current;
  174. Entity myentity = myTran.GetObject(myobjid, OpenMode.ForWrite) as Entity;
  175. //////测试后台可否执行的代码
  176. ////myentity.Color = Autodesk.AutoCAD.Colors.Color.FromColor(System.Drawing.Color.Blue);
  177. ////myentity.ColorIndex = 5;
  178. //////修改文字代码
  179. if (myentity is DBText)
  180. {
  181. DBText text = myentity as DBText;
  182. string str = text.TextString;
  183. switch (str)
  184. {
  185. case "aaaa":
  186. break;
  187. case "bbbb":
  188. break;
  189. case "cccc":
  190. break;
  191. case "eeee":
  192. break;
  193. case "ffff":
  194. break;
  195. case "xxxx":
  196. break;
  197. case "yyyy":
  198. break;
  199. case "zzzz":
  200. break;
  201. //case "Name":
  202. // text.TextString = "邻水县农村土地经营权DOM"; //"邻水县" + "XXX镇XXX村" + "农村土地经营权DOM";
  203. // break;
  204. case "ididid":
  205. text.TextString = myName;
  206. break;
  207. case "xlt":
  208. text.TextString = xltstr.Substring(0, xltstr.Length - 4);
  209. break;
  210. case "xlt2":
  211. text.TextString = xltstr.Substring(xltstr.Length - 4, 4);
  212. break;
  213. case "ylt":
  214. text.TextString = yltstr.Substring(0,yltstr .Length -5);
  215. break;
  216. case "ylt2":
  217. text.TextString = yltstr.Substring(yltstr.Length - 5, 5);
  218. break;
  219. case "xrt":
  220. text.TextString = xrtstr.Substring(0, xrtstr.Length - 4);
  221. break;
  222. case "xrt2":
  223. text.TextString = xrtstr.Substring(xrtstr.Length - 4, 4);
  224. break;
  225. case "yrt":
  226. text.TextString = yrtstr.Substring(0, yrtstr.Length - 5);
  227. break;
  228. case "yrt2":
  229. text.TextString = yrtstr.Substring(yrtstr.Length - 5, 5);
  230. break;
  231. case "xlb":
  232. text.TextString = xlbstr.Substring(0, xlbstr.Length - 4);
  233. break;
  234. case "xlb2":
  235. text.TextString = xlbstr.Substring(xlbstr.Length - 4, 4);
  236. break;
  237. case "ylb":
  238. text.TextString = ylbstr.Substring(0, ylbstr.Length - 5);
  239. break;
  240. case "ylb2":
  241. text.TextString = ylbstr.Substring( ylbstr.Length - 5,5);
  242. break;
  243. case "xrb":
  244. text.TextString = xrbstr.Substring(0, xrtstr.Length - 4);
  245. break;
  246. case "xrb2":
  247. text.TextString = xrbstr.Substring(xrbstr.Length - 4, 4);
  248. break;
  249. case "yrb":
  250. text.TextString = yrbstr.Substring(0, yrbstr.Length - 5);
  251. break;
  252. case "yrb2":
  253. text.TextString = yrbstr.Substring(yrbstr.Length - 5, 5);
  254. break;
  255. //case "":
  256. // break;
  257. default: break;
  258. }
  259. myentity = text;
  260. }
  261. Point3d basept = new Point3d(0, 0, 0);
  262. Point3d targetpt = new Point3d(xmove, ymove, 0);
  263. Vector3d vec = targetpt - basept;
  264. Matrix3d mt3d = Matrix3d.Displacement(vec);
  265. myentity.TransformBy(mt3d);
  266. }
  267. btrenu.Dispose();
  268. btr.Dispose();
  269. }
  270. bt.Dispose();
  271. myTran.Commit();
  272. myTran.Dispose();
  273. }
  274. catch (Exception ex)
  275. {
  276. MessageBox.Show(ex.ToString ());
  277. }
  278. }
  279. //////DOM
  280. //using (var myTran = myDB.TransactionManager.StartTransaction())
  281. //{
  282. // try
  283. // {
  284. // BlockTable blktbl = myTran.GetObject(myDB.BlockTableId, OpenMode.ForRead) as BlockTable;
  285. // BlockTableRecord blktblrec = myTran.GetObject(blktbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  286. // DrawOrderTable dot = myTran.GetObject(blktblrec.DrawOrderTableId, OpenMode.ForWrite) as DrawOrderTable;
  287. // //////////////////////创建图像定义对象
  288. // ObjectId imgid = RasterImageDef.GetImageDictionary(myDB);
  289. // if (imgid == ObjectId.Null)
  290. // imgid = RasterImageDef.CreateImageDictionary(myDB);
  291. // DBDictionary imgdict = myTran.GetObject(imgid, OpenMode.ForWrite) as DBDictionary;
  292. // RasterImageDef imgdef = new RasterImageDef();
  293. // imgdef.SourceFileName = imageName;
  294. // imgdef.Load();
  295. // ObjectId imgdefid = imgdict.SetAt(imageName, imgdef);
  296. // myTran.AddNewlyCreatedDBObject(imgdef, true);
  297. // RasterImage image = new RasterImage();
  298. // image.ImageDefId = imgdefid;
  299. // //image.Layer = "DOM";
  300. // blktblrec.AppendEntity(image);
  301. // myTran.AddNewlyCreatedDBObject(image, true);
  302. // double height = imgdef.Size.Y;
  303. // Vector3d vt1 = scale * image.Orientation.Xaxis;
  304. // Vector3d vt2 = scale * image.Orientation.Yaxis;
  305. // image.AssociateRasterDef(imgdef);
  306. // image.Draw();
  307. // image.Orientation = new CoordinateSystem3d(new Point3d(X, Y - height * scale, 0), vt1, vt2);
  308. // ObjectIdCollection obidcoll = new ObjectIdCollection();
  309. // //影像后置
  310. // obidcoll.Add(image.ObjectId);
  311. // dot.MoveToBottom(obidcoll);
  312. // obidcoll = new ObjectIdCollection();
  313. // myTran.Commit();
  314. // myTran.Dispose();
  315. // //Autodesk.AutoCAD.ApplicationServices.Application.UpdateScreen();
  316. // MessageBox.Show(imageName + "影像已按照参数正确加载;");
  317. // }
  318. // catch (Exception ex)
  319. // {
  320. // MessageBox.Show(ex.ToString());
  321. // }
  322. //}
  323. ////保存为dwg code
  324. var resultfilename = dwgfilename;
  325. ////var resultfilename = @"D:\test\result\1000tk" + i.ToString() + ".dwg";
  326. myDB.SaveAs(resultfilename, DwgVersion.Current);
  327. //myDB.CloseInput(true);
  328. myDB.Dispose();
  329. }
  330. public void DWGXiugai()
  331. {
  332. FolderBrowserDialog fbd = new FolderBrowserDialog();
  333. fbd.Description = "请设置成果路径";
  334. if (fbd.ShowDialog() == DialogResult.OK)
  335. {
  336. string pathdirectory = fbd.SelectedPath;
  337. DirectoryInfo di = new DirectoryInfo(pathdirectory);
  338. FileInfo[] fiarray = di.GetFiles();
  339. foreach (FileInfo fi in fiarray)
  340. {
  341. string filename = fi.FullName;
  342. if (filename.Contains(".dwg"))
  343. {
  344. //DOMAndDWGfunction(fi);
  345. DWGTextXiugai(fi);
  346. }
  347. }
  348. }
  349. MessageBox.Show("程序运行结束");
  350. }
  351. public void DWGTextXiugai(FileInfo fi)
  352. {
  353. var filenamemuban = fi.FullName;
  354. Database myDB = new Database(false, true);
  355. myDB.ReadDwgFile(filenamemuban, System.IO.FileShare.ReadWrite, false, "");
  356. //myDB.CloseInput(true);
  357. ////选中所有对象进行平移
  358. using (var myTran = myDB.TransactionManager.StartTransaction())
  359. {
  360. try
  361. {
  362. var bt = (BlockTable)(myTran.GetObject(myDB.BlockTableId, OpenMode.ForWrite));
  363. //int count = 0;
  364. foreach (ObjectId blid in bt)
  365. {
  366. var btr = (BlockTableRecord)(myTran.GetObject(blid, OpenMode.ForWrite));
  367. BlockTableRecordEnumerator btrenu = btr.GetEnumerator();
  368. while (btrenu.MoveNext())
  369. {
  370. ObjectId myobjid = btrenu.Current;
  371. Entity myentity = myTran.GetObject(myobjid, OpenMode.ForWrite) as Entity;
  372. //测试后台可否执行的代码
  373. //myentity.Color = Autodesk.AutoCAD.Colors.Color.FromColor(System.Drawing.Color.Blue);
  374. //myentity.ColorIndex = 5;
  375. ////修改文字代码
  376. if (myentity is DBText)
  377. {
  378. DBText text = myentity as DBText;
  379. string str = text.TextString;
  380. // switch (str)
  381. // {
  382. // // case " 201503":
  383. // // text.TextString = "";
  384. // // break;
  385. // // case "( )":
  386. // // text.TextString = "";
  387. // // break;
  388. // // ////////////////////////
  389. // // //case "邻水县八斗丘村农村土地经营权DOM":
  390. // // // text.TextString = "邻水县" + "柳塘乡"+"八斗坵村" + "农村土地承包经营权确权登记工作底图";
  391. // // // Point3d pt = text.Position;
  392. // // // text.Position = new Point3d(pt.X - 28, pt.Y, pt.Z);
  393. // // // break;
  394. // // //case "邻水县大步口村农村土地经营权DOM":
  395. // // // text.TextString = "邻水县" + "石滓乡" + "大步口村" + "农村土地承包经营权确权登记工作底图";
  396. // // // Point3d pt = text.Position;
  397. // // // text.Position = new Point3d(pt.X - 28, pt.Y, pt.Z);
  398. // // // break;
  399. // // //case "邻水县大河坝村农村土地经营权DOM":
  400. // // // text.TextString = "邻水县" + "柳塘乡" + "大河坝村" + "农村土地承包经营权确权登记工作底图";
  401. // // // Point3d pt = text.Position;
  402. // // // text.Position = new Point3d(pt.X - 28, pt.Y, pt.Z);
  403. // // // break;
  404. // // //case "邻水县沙帽石村农村土地经营权DOM":
  405. // // // text.TextString = "邻水县" + "柳塘乡" + "沙帽石村" + "农村土地承包经营权确权登记工作底图";
  406. // // // Point3d pt = text.Position;
  407. // // // text.Position = new Point3d(pt.X - 28, pt.Y, pt.Z);
  408. // // // break;
  409. // // //case "邻水县双桥居委会农村土地经营权DOM":
  410. // // // text.TextString = "邻水县" + "柳塘乡" + "双桥居委会" + "农村土地承包经营权确权登记工作底图";
  411. // // // Point3d pt = text.Position;
  412. // // // text.Position = new Point3d(pt.X - 28, pt.Y, pt.Z);
  413. // // // break;
  414. // // //case "邻水县雄码头村农村土地经营权DOM":
  415. // // // text.TextString = "邻水县" + "柳塘乡" + "雄码头村" + "农村土地承包经营权确权登记工作底图";
  416. // // // Point3d pt = text.Position;
  417. // // // text.Position = new Point3d(pt.X - 28, pt.Y, pt.Z);
  418. // // // break;
  419. // // //////////////////
  420. // // case "邻水县袁市镇增产村农村土地经营权DOM":
  421. // // text.TextString = "邻水县" + "袁市镇" + "增产村" + "农村土地承包经营权确权登记工作底图";
  422. // // Point3d pt = text.Position;
  423. // // text.Position = new Point3d(pt.X - 28, pt.Y, pt.Z);
  424. // // break;
  425. // // case "投影方式:高斯投影。中央子午线106.5度。":
  426. // // text.TextString = "国家2000坐标系。";
  427. // // break;
  428. // // case "国家2000坐标系。":
  429. // // text.TextString = "投影方式:高斯投影,中央子午线经度106.5。";
  430. // // break;
  431. // // case "1985年黄海高程系。":
  432. // // text.TextString = "1985国家高程系统。";
  433. // // break;
  434. // // case "2015年03月数字成图。":
  435. // // text.TextString = "影像2015年01月摄。";
  436. // // break;
  437. // // case "测图员 ":
  438. // // text.TextString = "调绘者: ";
  439. // // break;
  440. // // case "土调员 ":
  441. // // text.TextString = "指界人: ";
  442. // // break;
  443. // // case "数化员 ":
  444. // // text.TextString = "组长联系方式:";
  445. // // break;
  446. // // case "检查员 ":
  447. // // text.TextString = "";
  448. // // break;
  449. // // case "复查员 ":
  450. // // text.TextString = "";
  451. // // break;
  452. // ////////////////////////////
  453. // /////////////////////////////
  454. // //case "投影方式:高斯投影。":
  455. // // text.TextString = "国家2000坐标系。";
  456. // // break;
  457. // //case "国家2000坐标系。":
  458. // // text.TextString = "投影方式:高斯投影,中央子午线经度106.5。";
  459. // // break;
  460. // //case "1985年黄海高程系。":
  461. // // text.TextString = "1985国家高程系统。";
  462. // // break;
  463. // //case "2015年03月数字成图。":
  464. // // text.TextString = "影像2015年01月摄。";
  465. // // break;
  466. // //case "数化员 陈步清":
  467. // // text.TextString = "调绘者: ";
  468. // // break;
  469. // //case "检查员 李 敏":
  470. // // text.TextString = "指界人: ";
  471. // // break;
  472. // //case "复查员 ":
  473. // // text.TextString = "组长联系方式:";
  474. // // break;
  475. // case "影像2015年01月摄。":
  476. // text.TextString = "2015年01月摄。";
  477. // break;
  478. // default: break;
  479. // }
  480. switch (str)
  481. {
  482. //case "投影方式:高斯投影,中央子午线经度106.5。":
  483. // if (count == 0)
  484. // text.TextString = "国家2000坐标系。";
  485. // count++;
  486. // break ;
  487. //case "投影方式:高斯投影,中央子午线经度106.5。":
  488. // text.TextString = "国家2000坐标系。";
  489. // break;
  490. //case "国家2000坐标系。":
  491. // text.TextString = "投影方式:高斯投影,中央子午线经度106.5。";
  492. // break;
  493. case "邻水县八斗坵村农村土地经营权DOM":
  494. text.TextString = "邻水县柳塘乡八斗坵村农村土地承包经营权确权登记工作底图";
  495. break;
  496. default :break ;
  497. }
  498. myentity = text;
  499. }
  500. }
  501. btrenu.Dispose();
  502. btr.Dispose();
  503. }
  504. bt.Dispose();
  505. myTran.Commit();
  506. myTran.Dispose();
  507. }
  508. catch (Exception ex)
  509. {
  510. MessageBox.Show(ex.ToString());
  511. }
  512. }
  513. ////保存为dwg code
  514. //var resultfilename = dwgfilename;
  515. ////var resultfilename = @"D:\test\result\1000tk" + i.ToString() + ".dwg";
  516. myDB.SaveAs(filenamemuban, DwgVersion.Current);
  517. //myDB.CloseInput(true);
  518. myDB.Dispose();
  519. }
  520. public void openfunction()
  521. {
  522. try
  523. {
  524. //for (int i = 0; i < 10; i++)
  525. //{
  526. var filenamemuban = @"D:\test\1000tk.dwg";
  527. //Document document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  528. var docMgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
  529. var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(filenamemuban);
  530. docMgr.MdiActiveDocument = doc;
  531. //doc.Editor.WriteMessage("文档已打开0.0");
  532. var resultfilename = @"D:\test\result\1.dwg";
  533. //var resultfilename = @"D:\test\result\1000tk"+i.ToString ()+".dwg";
  534. //doc.CloseAndSave(resultfilename);
  535. //Database db= doc.Database;
  536. Database database = HostApplicationServices.WorkingDatabase;
  537. database.SaveAs(resultfilename, DwgVersion.Current);
  538. //}
  539. }
  540. catch (Exception ex)
  541. {
  542. MessageBox.Show(ex.ToString());
  543. }
  544. }
  545. /// <summary>
  546. /// 20150611code
  547. /// </summary>
  548. public void DGWTKfunctionnew ()
  549. {
  550. //OpenFileDialog ofd = new OpenFileDialog();
  551. //if (ofd.ShowDialog() == DialogResult.OK)
  552. //{
  553. char[] chspilt = new char[] { ',' };
  554. try
  555. {
  556. //string fileresult = ofd.FileName;
  557. string fileresult = @"H:\2015Work\20150309DOMLS_TKCode\moban\myResult.txt";
  558. string outfilapth = @"H:\2015Work\20150309DOMLS_TKCode\DWGResult\";
  559. StreamReader sr = new StreamReader(fileresult);
  560. ////////
  561. int i = 0;
  562. while (sr.Peek() != -1)
  563. {
  564. //i++;
  565. //if (i > 2)
  566. // break;
  567. string temp = sr.ReadLine();
  568. string[] spilt = temp.Split(chspilt, StringSplitOptions.RemoveEmptyEntries);
  569. if (spilt.Length >= 11)
  570. {
  571. string name = spilt[0];
  572. double minX = Convert.ToDouble(spilt[1]);
  573. double minY = Convert.ToDouble(spilt[2]);
  574. string name1 = spilt[3];
  575. string name2 = spilt[4];
  576. string name3 = spilt[5];
  577. string name4 = spilt[6];
  578. string name5 = spilt[7];
  579. string name6 = spilt[8];
  580. string name7 = spilt[9];
  581. string name8 = spilt[10];
  582. string dwgfilename = outfilapth + name+".dwg";
  583. {
  584. //移动dwg图框code
  585. //var filenamemuban = @"E:\2015Work\20150309DOM套图库程序\moban\1000lstk.dwg";
  586. var filenamemuban = @"H:\2015Work\20150309DOMLS_TKCode\moban\1000lstkcode.dwg";
  587. Database myDB = new Database(false, true);
  588. myDB.ReadDwgFile(filenamemuban, System.IO.FileShare.ReadWrite, false, "");
  589. myDB.CloseInput(true);
  590. //左下角坐标
  591. double xmove = minX;
  592. double ymove =minY;
  593. double len = 500;
  594. //左上 left top
  595. double xlt = minX / 1000.0;
  596. double ylt = (minY +len) / 1000.0;
  597. string xltstr = xlt.ToString("f1");
  598. string yltstr = ylt.ToString("f1");
  599. //右上 right top
  600. double xrt = (minX +len) / 1000.0;
  601. double yrt = (minY+len) / 1000.0;
  602. string xrtstr = xrt.ToString("f1");
  603. string yrtstr = yrt.ToString("f1");
  604. //左下 left bottom
  605. double xlb = minX / 1000.0;
  606. double ylb = minY/ 1000.0;
  607. string xlbstr = xlb.ToString("f1");
  608. string ylbstr = ylb.ToString("f1");
  609. //右下 right bottom
  610. double xrb = (minX+len) / 1000.0;
  611. double yrb = minY/ 1000.0;
  612. string xrbstr = xrb.ToString("f1");
  613. string yrbstr = yrb.ToString("f1");
  614. ////X = 559000.000 Y = 3349500.000 Z = 2874.000
  615. ////左上角坐标除去外扩
  616. //double xmove = X + 10;
  617. //double ymove = Y - 10;
  618. ////左上 left top
  619. //double xlt = xmove / 1000.0;
  620. //double ylt = ymove / 1000.0;
  621. //string xltstr = xlt.ToString("f1");
  622. //string yltstr = ylt.ToString("f1");
  623. ////右上 right top
  624. //double xrt = (xmove + 500) / 1000.0;
  625. //double yrt = ymove / 1000.0;
  626. //string xrtstr = xrt.ToString("f1");
  627. //string yrtstr = yrt.ToString("f1");
  628. ////左下 left bottom
  629. //double xlb = xmove / 1000.0;
  630. //double ylb = (ymove - 500) / 1000.0;
  631. //string xlbstr = xlb.ToString("f1");
  632. //string ylbstr = ylb.ToString("f1");
  633. ////右下 right bottom
  634. //double xrb = (xmove + 500) / 1000.0;
  635. //double yrb = (ymove - 500) / 1000.0;
  636. //string xrbstr = xrb.ToString("f1");
  637. //string yrbstr = yrb.ToString("f1");
  638. //////X = 559000.000 Y = 3349500.000 Z = 2874.000
  639. ////选中所有对象进行平移
  640. using (var myTran = myDB.TransactionManager.StartTransaction())
  641. {
  642. try
  643. {
  644. var bt = (BlockTable)(myTran.GetObject(myDB.BlockTableId, OpenMode.ForWrite));
  645. foreach (ObjectId blid in bt)
  646. {
  647. var btr = (BlockTableRecord)(myTran.GetObject(blid, OpenMode.ForWrite));
  648. BlockTableRecordEnumerator btrenu = btr.GetEnumerator();
  649. while (btrenu.MoveNext())
  650. {
  651. ObjectId myobjid = btrenu.Current;
  652. Entity myentity = myTran.GetObject(myobjid, OpenMode.ForWrite) as Entity;
  653. //////测试后台可否执行的代码
  654. ////myentity.Color = Autodesk.AutoCAD.Colors.Color.FromColor(System.Drawing.Color.Blue);
  655. ////myentity.ColorIndex = 5;
  656. //////修改文字代码
  657. if (myentity is DBText)
  658. {
  659. DBText text = myentity as DBText;
  660. string str = text.TextString;
  661. switch (str)
  662. {
  663. case "name1":
  664. if (name1 == "NULL")
  665. text.TextString = " ";
  666. else
  667. text.TextString =name1;
  668. break;
  669. case "name2":
  670. if (name2 == "NULL")
  671. text.TextString = " ";
  672. else
  673. text.TextString =name2;
  674. break;
  675. case "name3":
  676. if (name3 == "NULL")
  677. text.TextString = " ";
  678. else
  679. text.TextString =name3;
  680. break;
  681. case "name4":
  682. if (name4 == "NULL")
  683. text.TextString = " ";
  684. else
  685. text.TextString =name4;
  686. break;
  687. case "name5":
  688. if (name5 == "NULL")
  689. text.TextString = " ";
  690. else
  691. text.TextString =name5;
  692. break;
  693. case "name6":
  694. if (name6 == "NULL")
  695. text.TextString = " ";
  696. else
  697. text.TextString = name6;
  698. break;
  699. case "name7":
  700. if (name7 == "NULL")
  701. text.TextString = " ";
  702. else
  703. text.TextString =name7;
  704. break;
  705. case "name8":
  706. if (name8 == "NULL")
  707. text.TextString = " ";
  708. else
  709. text.TextString =name8;
  710. break;
  711. //case "Name":
  712. // text.TextString = "邻水县农村土地经营权DOM"; //"邻水县" + "XXX镇XXX村" + "农村土地经营权DOM";
  713. // break;
  714. case "(ididid)":
  715. text.TextString = "("+name+")";
  716. break;
  717. case "xlt":
  718. text.TextString = xltstr.Substring(0, xltstr.Length - 4);
  719. break;
  720. case "xlt2":
  721. text.TextString = xltstr.Substring(xltstr.Length - 4, 4);
  722. break;
  723. case "ylt":
  724. text.TextString = yltstr.Substring(0, yltstr.Length - 5);
  725. break;
  726. case "ylt2":
  727. text.TextString = yltstr.Substring(yltstr.Length - 5, 5);
  728. break;
  729. case "xrt":
  730. text.TextString = xrtstr.Substring(0, xrtstr.Length - 4);
  731. break;
  732. case "xrt2":
  733. text.TextString = xrtstr.Substring(xrtstr.Length - 4, 4);
  734. break;
  735. case "yrt":
  736. text.TextString = yrtstr.Substring(0, yrtstr.Length - 5);
  737. break;
  738. case "yrt2":
  739. text.TextString = yrtstr.Substring(yrtstr.Length - 5, 5);
  740. break;
  741. case "xlb":
  742. text.TextString = xlbstr.Substring(0, xlbstr.Length - 4);
  743. break;
  744. case "xlb2":
  745. text.TextString = xlbstr.Substring(xlbstr.Length - 4, 4);
  746. break;
  747. case "ylb":
  748. text.TextString = ylbstr.Substring(0, ylbstr.Length - 5);
  749. break;
  750. case "ylb2":
  751. text.TextString = ylbstr.Substring(ylbstr.Length - 5, 5);
  752. break;
  753. case "xrb":
  754. text.TextString = xrbstr.Substring(0, xrtstr.Length - 4);
  755. break;
  756. case "xrb2":
  757. text.TextString = xrbstr.Substring(xrbstr.Length - 4, 4);
  758. break;
  759. case "yrb":
  760. text.TextString = yrbstr.Substring(0, yrbstr.Length - 5);
  761. break;
  762. case "yrb2":
  763. text.TextString = yrbstr.Substring(yrbstr.Length - 5, 5);
  764. break;
  765. //case "":
  766. // break;
  767. default: break;
  768. }
  769. myentity = text;
  770. }
  771. Point3d basept = new Point3d(0, 0, 0);
  772. Point3d targetpt = new Point3d(xmove, ymove, 0);
  773. Vector3d vec = targetpt - basept;
  774. Matrix3d mt3d = Matrix3d.Displacement(vec);
  775. myentity.TransformBy(mt3d);
  776. }
  777. btrenu.Dispose();
  778. btr.Dispose();
  779. }
  780. bt.Dispose();
  781. myTran.Commit();
  782. myTran.Dispose();
  783. }
  784. catch (Exception ex)
  785. {
  786. MessageBox.Show(ex.ToString());
  787. }
  788. }
  789. //////DOM
  790. //using (var myTran = myDB.TransactionManager.StartTransaction())
  791. //{
  792. // try
  793. // {
  794. // BlockTable blktbl = myTran.GetObject(myDB.BlockTableId, OpenMode.ForRead) as BlockTable;
  795. // BlockTableRecord blktblrec = myTran.GetObject(blktbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  796. // DrawOrderTable dot = myTran.GetObject(blktblrec.DrawOrderTableId, OpenMode.ForWrite) as DrawOrderTable;
  797. // //////////////////////创建图像定义对象
  798. // ObjectId imgid = RasterImageDef.GetImageDictionary(myDB);
  799. // if (imgid == ObjectId.Null)
  800. // imgid = RasterImageDef.CreateImageDictionary(myDB);
  801. // DBDictionary imgdict = myTran.GetObject(imgid, OpenMode.ForWrite) as DBDictionary;
  802. // RasterImageDef imgdef = new RasterImageDef();
  803. // imgdef.SourceFileName = imageName;
  804. // imgdef.Load();
  805. // ObjectId imgdefid = imgdict.SetAt(imageName, imgdef);
  806. // myTran.AddNewlyCreatedDBObject(imgdef, true);
  807. // RasterImage image = new RasterImage();
  808. // image.ImageDefId = imgdefid;
  809. // //image.Layer = "DOM";
  810. // blktblrec.AppendEntity(image);
  811. // myTran.AddNewlyCreatedDBObject(image, true);
  812. // double height = imgdef.Size.Y;
  813. // Vector3d vt1 = scale * image.Orientation.Xaxis;
  814. // Vector3d vt2 = scale * image.Orientation.Yaxis;
  815. // image.AssociateRasterDef(imgdef);
  816. // image.Draw();
  817. // image.Orientation = new CoordinateSystem3d(new Point3d(X, Y - height * scale, 0), vt1, vt2);
  818. // ObjectIdCollection obidcoll = new ObjectIdCollection();
  819. // //影像后置
  820. // obidcoll.Add(image.ObjectId);
  821. // dot.MoveToBottom(obidcoll);
  822. // obidcoll = new ObjectIdCollection();
  823. // myTran.Commit();
  824. // myTran.Dispose();
  825. // //Autodesk.AutoCAD.ApplicationServices.Application.UpdateScreen();
  826. // MessageBox.Show(imageName + "影像已按照参数正确加载;");
  827. // }
  828. // catch (Exception ex)
  829. // {
  830. // MessageBox.Show(ex.ToString());
  831. // }
  832. //}
  833. ////保存为dwg code
  834. var resultfilename = dwgfilename;
  835. ////var resultfilename = @"D:\test\result\1000tk" + i.ToString() + ".dwg";
  836. myDB.SaveAs(resultfilename, DwgVersion.Current);
  837. //myDB.CloseInput(true);
  838. myDB.Dispose();
  839. }
  840. }
  841. }
  842. }
  843. catch (Exception ex0)
  844. {
  845. MessageBox.Show(ex0.ToString());
  846. }
  847. //}
  848. //else
  849. // return;
  850. }
  851. ///end
  852. ///
  853. public string str3(string strpama)
  854. {
  855. if (strpama == "NULL")
  856. { return strpama; }
  857. else
  858. { return strpama.Substring(strpama.Length - 3); }
  859. }
  860. /// <summary>
  861. /// 20200807code
  862. /// 20230301 update
  863. /// </summary>
  864. public void DGWTKfunction2020()
  865. {
  866. //OpenFileDialog ofd = new OpenFileDialog();
  867. //if (ofd.ShowDialog() == DialogResult.OK)
  868. //{
  869. char[] chspilt = new char[] { ',' };
  870. try
  871. {
  872. Document document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  873. Database database = HostApplicationServices.WorkingDatabase;
  874. PromptStringOptions pStrOpts = new PromptStringOptions("\nEnter daihao 代号(默认无代号): ");
  875. pStrOpts.AllowSpaces = true;
  876. PromptResult pStrRes = document.Editor.GetString(pStrOpts);
  877. //代号
  878. string ssssssss = pStrRes.StringResult;
  879. //MessageBox.Show(ssssssss);
  880. pStrOpts = new PromptStringOptions("\nEnter 小数位数(默认为2位): ");
  881. pStrOpts.AllowSpaces = true;
  882. pStrRes = document.Editor.GetString(pStrOpts);
  883. //小数位数
  884. string xsws = pStrRes.StringResult;
  885. string ffff111222="f2";
  886. if (xsws == "1")
  887. ffff111222 = "f1";
  888. //////////////////////////////////////////////////////
  889. //////////////////////////////////////////////////////
  890. //string fileresult = ofd.FileName;
  891. string fileresult = "";
  892. string filenamemuban = "";
  893. OpenFileDialog ofd = new OpenFileDialog();
  894. ofd.Title = "请选择导出的txt文件";
  895. if (ofd.ShowDialog() == DialogResult.OK)
  896. {
  897. fileresult = ofd.FileName;
  898. }
  899. else
  900. return;
  901. ofd.Dispose();
  902. OpenFileDialog od222 = new OpenFileDialog();
  903. od222.Title = "请选择模板.dwg文件";
  904. if (od222.ShowDialog() == DialogResult.OK)
  905. {
  906. filenamemuban = od222.FileName;
  907. }
  908. else
  909. return;
  910. od222.Dispose();
  911. string outfilapth = Path.GetDirectoryName(fileresult);
  912. StreamReader sr = new StreamReader(fileresult);
  913. ////////
  914. int i = 0;
  915. while (sr.Peek() != -1)
  916. {
  917. //i++;
  918. //if (i > 2)
  919. // break;
  920. string temp = sr.ReadLine();
  921. string[] spilt = temp.Split(chspilt, StringSplitOptions.RemoveEmptyEntries);
  922. if (spilt.Length >= 11)
  923. {
  924. string name = spilt[0];
  925. double minX = Convert.ToDouble(spilt[1]);
  926. double minY = Convert.ToDouble(spilt[2]);
  927. double maxX = Convert.ToDouble(spilt[3]);
  928. double maxY = Convert.ToDouble(spilt[4]);
  929. string name1 = spilt[5];
  930. string name2 = spilt[6];
  931. string name3 = spilt[7];
  932. string name4 = spilt[8];
  933. string name5 = spilt[9];
  934. string name6 = spilt[10];
  935. string name7 = spilt[11];
  936. string name8 = spilt[12];
  937. /////////////////////////
  938. /////////////////////////
  939. string dwgfilename = outfilapth + "\\"+name + ".dwg";
  940. //MessageBox.Show(dwgfilename);
  941. {
  942. //移动dwg图框code
  943. //var filenamemuban = @"E:\2015Work\20150309DOM套图库程序\moban\1000lstk.dwg";
  944. Database myDB = new Database(false, true);
  945. myDB.ReadDwgFile(filenamemuban, System.IO.FileShare.ReadWrite, false, "");
  946. myDB.CloseInput(true);
  947. //左下角坐标
  948. double xmove = minX;
  949. double ymove = minY;
  950. Point3d basept = new Point3d(0, 0, 0);
  951. Point3d targetpt = new Point3d(xmove, ymove, 0);
  952. Vector3d vec = targetpt - basept;
  953. Matrix3d mt3d = Matrix3d.Displacement(vec);
  954. //double len = 500;
  955. ////左上 left top 2020 code
  956. //double xlt = minX / 1000.0;
  957. //double ylt = maxY / 1000.0;
  958. //string xltstr = xlt.ToString("f1");
  959. //string yltstr = ylt.ToString("f1");
  960. ////右上 right top
  961. //double xrt = maxX / 1000.0;
  962. //double yrt = maxY / 1000.0;
  963. //string xrtstr = xrt.ToString("f1");
  964. //string yrtstr = yrt.ToString("f1");
  965. ////左下 left bottom
  966. //double xlb = minX / 1000.0;
  967. //double ylb = minY / 1000.0;
  968. //string xlbstr = xlb.ToString("f1");
  969. //string ylbstr = ylb.ToString("f1");
  970. ////右下 right bottom
  971. //double xrb =maxX / 1000.0;
  972. //double yrb = minY / 1000.0;
  973. //string xrbstr = xrb.ToString("f1");
  974. //string yrbstr = yrb.ToString("f1");
  975. ////左上 left top
  976. //double xlt = minX / 1000.0;
  977. //double ylt = maxY / 1000.0;
  978. //string xltstr = xlt.ToString("f2");
  979. //string yltstr = ylt.ToString("f2");
  980. ////右上 right top
  981. //double xrt = maxX / 1000.0;
  982. //double yrt = maxY / 1000.0;
  983. //string xrtstr = xrt.ToString("f2");
  984. //string yrtstr = yrt.ToString("f2");
  985. ////左下 left bottom
  986. //double xlb = minX / 1000.0;
  987. //double ylb = minY / 1000.0;
  988. //string xlbstr = xlb.ToString("f2");
  989. //string ylbstr = ylb.ToString("f2");
  990. ////右下 right bottom
  991. //double xrb = maxX / 1000.0;
  992. //double yrb = minY / 1000.0;
  993. //string xrbstr = xrb.ToString("f2");
  994. //string yrbstr = yrb.ToString("f2");
  995. //左上 left top 2021 code
  996. double xlt = minX / 1000.0;
  997. double ylt = maxY / 1000.0;
  998. string xltstr = xlt.ToString(ffff111222);
  999. string yltstr = ylt.ToString(ffff111222);
  1000. //右上 right top
  1001. double xrt = maxX / 1000.0;
  1002. double yrt = maxY / 1000.0;
  1003. string xrtstr = xrt.ToString(ffff111222);
  1004. string yrtstr = yrt.ToString(ffff111222);
  1005. //左下 left bottom
  1006. double xlb = minX / 1000.0;
  1007. double ylb = minY / 1000.0;
  1008. string xlbstr = xlb.ToString(ffff111222);
  1009. string ylbstr = ylb.ToString(ffff111222);
  1010. //右下 right bottom
  1011. double xrb = maxX / 1000.0;
  1012. double yrb = minY / 1000.0;
  1013. string xrbstr = xrb.ToString(ffff111222);
  1014. string yrbstr = yrb.ToString(ffff111222);
  1015. //////////////////////////////////////////////////////
  1016. //////////////////////////////////////////////////////
  1017. string daihao = ssssssss;
  1018. //////////////////////////////////////////////////////
  1019. //////////////////////////////////////////////////////
  1020. #region//old 2020 code
  1021. //////选中所有对象进行平移
  1022. //using (var myTran = myDB.TransactionManager.StartTransaction())
  1023. //{
  1024. // try
  1025. // {
  1026. // var bt = (BlockTable)(myTran.GetObject(myDB.BlockTableId, OpenMode.ForWrite));
  1027. // foreach (ObjectId blid in bt)
  1028. // {
  1029. // var btr = (BlockTableRecord)(myTran.GetObject(blid, OpenMode.ForWrite));
  1030. // BlockTableRecordEnumerator btrenu = btr.GetEnumerator();
  1031. // while (btrenu.MoveNext())
  1032. // {
  1033. // ObjectId myobjid = btrenu.Current;
  1034. // Entity myentity = myTran.GetObject(myobjid, OpenMode.ForWrite) as Entity;
  1035. // //////测试后台可否执行的代码
  1036. // ////myentity.Color = Autodesk.AutoCAD.Colors.Color.FromColor(System.Drawing.Color.Blue);
  1037. // ////myentity.ColorIndex = 5;
  1038. // //////修改文字代码
  1039. // if (myentity is DBText)
  1040. // {
  1041. // DBText text = myentity as DBText;
  1042. // string str = text.TextString;
  1043. // #region
  1044. // switch (str)
  1045. // {
  1046. // case "name1":
  1047. // if (name1 == "NULL")
  1048. // text.TextString = " ";
  1049. // else
  1050. // text.TextString = name1;
  1051. // break;
  1052. // case "name2":
  1053. // if (name2 == "NULL")
  1054. // text.TextString = " ";
  1055. // else
  1056. // text.TextString = name2;
  1057. // break;
  1058. // case "name3":
  1059. // if (name3 == "NULL")
  1060. // text.TextString = " ";
  1061. // else
  1062. // text.TextString = name3;
  1063. // break;
  1064. // case "name4":
  1065. // if (name4 == "NULL")
  1066. // text.TextString = " ";
  1067. // else
  1068. // text.TextString = name4;
  1069. // break;
  1070. // case "name5":
  1071. // if (name5 == "NULL")
  1072. // text.TextString = " ";
  1073. // else
  1074. // text.TextString = name5;
  1075. // break;
  1076. // case "name6":
  1077. // if (name6 == "NULL")
  1078. // text.TextString = " ";
  1079. // else
  1080. // text.TextString = name6;
  1081. // break;
  1082. // case "name7":
  1083. // if (name7 == "NULL")
  1084. // text.TextString = " ";
  1085. // else
  1086. // text.TextString = name7;
  1087. // break;
  1088. // case "name8":
  1089. // if (name8 == "NULL")
  1090. // text.TextString = " ";
  1091. // else
  1092. // text.TextString = name8;
  1093. // break;
  1094. // //case "Name":
  1095. // // text.TextString = "邻水县农村土地经营权DOM"; //"邻水县" + "XXX镇XXX村" + "农村土地经营权DOM";
  1096. // // break;
  1097. // //case "ididid":
  1098. // // //text.TextString = "(" + name + ")";
  1099. // // //text.TextString = name .Substring(name.Length - 3);
  1100. // // text.TextString = name;
  1101. // // break;
  1102. // case "(ididid)":
  1103. // //text.TextString = "(" + name + ")";
  1104. // //text.TextString = name .Substring(name.Length - 3);
  1105. // text.TextString = "("+name+")";
  1106. // break;
  1107. // case "xlt":
  1108. // //text.TextString =daihao+ xltstr.Substring(0, xltstr.Length - 4);
  1109. // text.TextString = daihao + xltstr.Substring(0, xltstr.Length - 5);
  1110. // break;
  1111. // case "xlt2":
  1112. // //text.TextString = xltstr.Substring(xltstr.Length - 4, 4);
  1113. // text.TextString = xltstr.Substring(xltstr.Length - 5, 5);
  1114. // break;
  1115. // case "ylt":
  1116. // text.TextString = yltstr.Substring(0, yltstr.Length - 5);
  1117. // //text.TextString = yltstr.Substring(0, yltstr.Length - 4);
  1118. // break;
  1119. // case "ylt2":
  1120. // text.TextString = yltstr.Substring(yltstr.Length - 5, 5);
  1121. // //text.TextString = yltstr.Substring(yltstr.Length - 4, 4);
  1122. // break;
  1123. // case "xrt":
  1124. // //text.TextString = daihao + xrtstr.Substring(0, xrtstr.Length - 4);
  1125. // text.TextString = daihao + xrtstr.Substring(0, xrtstr.Length - 5);
  1126. // break;
  1127. // case "xrt2":
  1128. // //text.TextString = xrtstr.Substring(xrtstr.Length - 4, 4);
  1129. // text.TextString = xrtstr.Substring(xrtstr.Length - 5, 5);
  1130. // break;
  1131. // case "yrt":
  1132. // text.TextString = yrtstr.Substring(0, yrtstr.Length - 5);
  1133. // //text.TextString = yrtstr.Substring(0, yrtstr.Length - 4);
  1134. // break;
  1135. // case "yrt2":
  1136. // text.TextString = yrtstr.Substring(yrtstr.Length - 5, 5);
  1137. // //text.TextString = yrtstr.Substring(yrtstr.Length - 4, 4);
  1138. // break;
  1139. // case "xlb":
  1140. // //text.TextString = daihao + xlbstr.Substring(0, xlbstr.Length - 4);
  1141. // text.TextString = daihao + xlbstr.Substring(0, xlbstr.Length - 5);
  1142. // break;
  1143. // case "xlb2":
  1144. // //text.TextString = xlbstr.Substring(xlbstr.Length - 4, 4);
  1145. // text.TextString = xlbstr.Substring(xlbstr.Length - 5, 5);
  1146. // break;
  1147. // case "ylb":
  1148. // text.TextString = ylbstr.Substring(0, ylbstr.Length - 5);
  1149. // //text.TextString = ylbstr.Substring(0, ylbstr.Length - 4);
  1150. // break;
  1151. // case "ylb2":
  1152. // text.TextString = ylbstr.Substring(ylbstr.Length - 5, 5);
  1153. // //text.TextString = ylbstr.Substring(ylbstr.Length - 4, 4);
  1154. // break;
  1155. // case "xrb":
  1156. // //text.TextString = daihao + xrbstr.Substring(0, xrtstr.Length - 4);
  1157. // text.TextString = daihao + xrbstr.Substring(0, xrtstr.Length - 5);
  1158. // break;
  1159. // case "xrb2":
  1160. // //text.TextString = xrbstr.Substring(xrbstr.Length - 4, 4);
  1161. // text.TextString = xrbstr.Substring(xrbstr.Length - 5, 5);
  1162. // break;
  1163. // case "yrb":
  1164. // text.TextString = yrbstr.Substring(0, yrbstr.Length - 5);
  1165. // //text.TextString = yrbstr.Substring(0, yrbstr.Length - 4);
  1166. // break;
  1167. // case "yrb2":
  1168. // text.TextString = yrbstr.Substring(yrbstr.Length - 5, 5);
  1169. // //text.TextString = yrbstr.Substring(yrbstr.Length - 4, 4);
  1170. // break;
  1171. // //case "":
  1172. // // break;
  1173. // default: break;
  1174. // }
  1175. // #endregion
  1176. // myentity = text;
  1177. // }
  1178. // myentity.TransformBy(mt3d);
  1179. // }
  1180. // btrenu.Dispose();
  1181. // btr.Dispose();
  1182. // }
  1183. // bt.Dispose();
  1184. // myTran.Commit();
  1185. // myTran.Dispose();
  1186. // }
  1187. // catch (Exception ex)
  1188. // {
  1189. // MessageBox.Show(ex.ToString());
  1190. // }
  1191. //}
  1192. #endregion
  1193. #region//old 2021 2023 code
  1194. // 2023 edit code
  1195. ////选中所有对象进行平移
  1196. using (var myTran = myDB.TransactionManager.StartTransaction())
  1197. {
  1198. try
  1199. {
  1200. var bt = (BlockTable)(myTran.GetObject(myDB.BlockTableId, OpenMode.ForWrite));
  1201. foreach (ObjectId blid in bt)
  1202. {
  1203. var btr = (BlockTableRecord)(myTran.GetObject(blid, OpenMode.ForWrite));
  1204. BlockTableRecordEnumerator btrenu = btr.GetEnumerator();
  1205. while (btrenu.MoveNext())
  1206. {
  1207. ObjectId myobjid = btrenu.Current;
  1208. Entity myentity = myTran.GetObject(myobjid, OpenMode.ForWrite) as Entity;
  1209. //////测试后台可否执行的代码
  1210. ////myentity.Color = Autodesk.AutoCAD.Colors.Color.FromColor(System.Drawing.Color.Blue);
  1211. ////myentity.ColorIndex = 5;
  1212. //////修改文字代码
  1213. if (myentity is DBText)
  1214. {
  1215. DBText text = myentity as DBText;
  1216. string str = text.TextString;
  1217. #region
  1218. switch (str)
  1219. {
  1220. case "name1":
  1221. if (name1 == "NULL")
  1222. text.TextString = " ";
  1223. else
  1224. text.TextString = name1;
  1225. break;
  1226. case "name2":
  1227. if (name2 == "NULL")
  1228. text.TextString = " ";
  1229. else
  1230. text.TextString = name2;
  1231. break;
  1232. case "name3":
  1233. if (name3 == "NULL")
  1234. text.TextString = " ";
  1235. else
  1236. text.TextString = name3;
  1237. break;
  1238. case "name4":
  1239. if (name4 == "NULL")
  1240. text.TextString = " ";
  1241. else
  1242. text.TextString = name4;
  1243. break;
  1244. case "name5":
  1245. if (name5 == "NULL")
  1246. text.TextString = " ";
  1247. else
  1248. text.TextString = name5;
  1249. break;
  1250. case "name6":
  1251. if (name6 == "NULL")
  1252. text.TextString = " ";
  1253. else
  1254. text.TextString = name6;
  1255. break;
  1256. case "name7":
  1257. if (name7 == "NULL")
  1258. text.TextString = " ";
  1259. else
  1260. text.TextString = name7;
  1261. break;
  1262. case "name8":
  1263. if (name8 == "NULL")
  1264. text.TextString = " ";
  1265. else
  1266. text.TextString = name8;
  1267. break;
  1268. //case "Name":
  1269. // text.TextString = "邻水县农村土地经营权DOM"; //"邻水县" + "XXX镇XXX村" + "农村土地经营权DOM";
  1270. // break;
  1271. //case "ididid":
  1272. // //text.TextString = "(" + name + ")";
  1273. // //text.TextString = name .Substring(name.Length - 3);
  1274. // text.TextString = name;
  1275. // break;
  1276. case "(ididid)":
  1277. //text.TextString = "(" + name + ")";
  1278. //text.TextString = name .Substring(name.Length - 3);
  1279. text.TextString = "(" + name + ")";
  1280. break;
  1281. case "xlt":
  1282. //text.TextString = daihao + xltstr.Substring(0, xltstr.Length - 4);
  1283. text.TextString = daihao + xltstr.Substring(0, xltstr.Length - 5);
  1284. if (xsws == "1")
  1285. text.TextString = daihao + xltstr.Substring(0, xltstr.Length - 4);
  1286. break;
  1287. case "xlt2":
  1288. //text.TextString = xltstr.Substring(xltstr.Length - 4, 4);
  1289. text.TextString = xltstr.Substring(xltstr.Length - 5, 5);
  1290. if (xsws == "1")
  1291. text.TextString = xltstr.Substring(xltstr.Length - 4, 4);
  1292. break;
  1293. case "ylt":
  1294. text.TextString = yltstr.Substring(0, yltstr.Length - 5);
  1295. if (xsws == "1")
  1296. text.TextString = yltstr.Substring(0, yltstr.Length - 4);
  1297. //text.TextString = yltstr.Substring(0, yltstr.Length - 4);
  1298. break;
  1299. case "ylt2":
  1300. text.TextString = yltstr.Substring(yltstr.Length - 5, 5);
  1301. //text.TextString = yltstr.Substring(yltstr.Length - 4, 4);
  1302. if (xsws == "1")
  1303. text.TextString = yltstr.Substring(yltstr.Length - 4, 4);
  1304. break;
  1305. case "xrt":
  1306. //text.TextString = daihao + xrtstr.Substring(0, xrtstr.Length - 4);
  1307. text.TextString = daihao + xrtstr.Substring(0, xrtstr.Length - 5);
  1308. if (xsws == "1")
  1309. text.TextString = daihao + xrtstr.Substring(0, xrtstr.Length - 4);
  1310. break;
  1311. case "xrt2":
  1312. //text.TextString = xrtstr.Substring(xrtstr.Length - 4, 4);
  1313. text.TextString = xrtstr.Substring(xrtstr.Length - 5, 5);
  1314. if (xsws == "1")
  1315. text.TextString = xrtstr.Substring(xrtstr.Length - 4, 4);
  1316. break;
  1317. case "yrt":
  1318. text.TextString = yrtstr.Substring(0, yrtstr.Length - 5);
  1319. //text.TextString = yrtstr.Substring(0, yrtstr.Length - 4);
  1320. if (xsws == "1")
  1321. text.TextString = yrtstr.Substring(0, yrtstr.Length - 4);
  1322. break;
  1323. case "yrt2":
  1324. text.TextString = yrtstr.Substring(yrtstr.Length - 5, 5);
  1325. //text.TextString = yrtstr.Substring(yrtstr.Length - 4, 4);
  1326. if (xsws == "1")
  1327. text.TextString = yrtstr.Substring(yrtstr.Length - 4, 4);
  1328. break;
  1329. case "xlb":
  1330. //text.TextString = daihao + xlbstr.Substring(0, xlbstr.Length - 4);
  1331. text.TextString = daihao + xlbstr.Substring(0, xlbstr.Length - 5);
  1332. if (xsws == "1")
  1333. text.TextString = daihao + xlbstr.Substring(0, xlbstr.Length - 4);
  1334. break;
  1335. case "xlb2":
  1336. //text.TextString = xlbstr.Substring(xlbstr.Length - 4, 4);
  1337. text.TextString = xlbstr.Substring(xlbstr.Length - 5, 5);
  1338. if (xsws == "1")
  1339. text.TextString = xlbstr.Substring(xlbstr.Length - 4, 4);
  1340. break;
  1341. case "ylb":
  1342. text.TextString = ylbstr.Substring(0, ylbstr.Length - 5);
  1343. //text.TextString = ylbstr.Substring(0, ylbstr.Length - 4);
  1344. if (xsws == "1")
  1345. text.TextString = ylbstr.Substring(0, ylbstr.Length - 4);
  1346. break;
  1347. case "ylb2":
  1348. text.TextString = ylbstr.Substring(ylbstr.Length - 5, 5);
  1349. //text.TextString = ylbstr.Substring(ylbstr.Length - 4, 4);
  1350. if (xsws == "1")
  1351. text.TextString = ylbstr.Substring(ylbstr.Length - 4, 4);
  1352. break;
  1353. case "xrb":
  1354. //text.TextString = daihao + xrbstr.Substring(0, xrtstr.Length - 4);
  1355. text.TextString = daihao + xrbstr.Substring(0, xrtstr.Length - 5);
  1356. if (xsws == "1")
  1357. text.TextString = daihao + xrbstr.Substring(0, xrtstr.Length - 4);
  1358. break;
  1359. case "xrb2":
  1360. //text.TextString = xrbstr.Substring(xrbstr.Length - 4, 4);
  1361. text.TextString = xrbstr.Substring(xrbstr.Length - 5, 5);
  1362. if (xsws == "1")
  1363. text.TextString = xrbstr.Substring(xrbstr.Length - 4, 4);
  1364. break;
  1365. case "yrb":
  1366. text.TextString = yrbstr.Substring(0, yrbstr.Length - 5);
  1367. //text.TextString = yrbstr.Substring(0, yrbstr.Length - 4);
  1368. if (xsws == "1")
  1369. text.TextString = yrbstr.Substring(0, yrbstr.Length - 4);
  1370. break;
  1371. case "yrb2":
  1372. text.TextString = yrbstr.Substring(yrbstr.Length - 5, 5);
  1373. //text.TextString = yrbstr.Substring(yrbstr.Length - 4, 4);
  1374. if (xsws == "1")
  1375. text.TextString = yrbstr.Substring(yrbstr.Length - 4, 4);
  1376. break;
  1377. case "xltxx2":
  1378. string xltxx2222 = (Convert.ToDouble(xltstr) + 0.5).ToString(ffff111222);
  1379. text.TextString = xltxx2222.Substring(1, xltxx2222.Length - 1);
  1380. break;
  1381. case "xltxx3":
  1382. string xltxx3333=(Convert.ToDouble(xltstr) + 1).ToString(ffff111222);
  1383. text.TextString = xltxx3333.Substring(1, xltxx3333.Length - 1);
  1384. break;
  1385. case "xltxx4":
  1386. string xltxx4444=(Convert.ToDouble(xltstr) + 1.5).ToString(ffff111222);
  1387. text.TextString = xltxx4444.Substring(1, xltxx4444.Length - 1);
  1388. break;
  1389. case "xltxx5":
  1390. string xltxx5555=(Convert.ToDouble(xltstr) + 2).ToString(ffff111222);
  1391. text.TextString = xltxx5555.Substring(1, xltxx5555.Length - 1);
  1392. break;
  1393. case "ylbyy2":
  1394. string ylbyy2222=(Convert.ToDouble(ylbstr) + 0.5).ToString(ffff111222);
  1395. text.TextString = ylbyy2222.Substring(2, ylbyy2222.Length - 2);
  1396. break;
  1397. case "ylbyy3":
  1398. string ylbyy3333=(Convert.ToDouble(ylbstr) +1).ToString(ffff111222);
  1399. text.TextString = ylbyy3333.Substring(2, ylbyy3333.Length - 2);
  1400. break;
  1401. case "ylbyy4":
  1402. string ylbyy4444=(Convert.ToDouble(ylbstr) + 1.5).ToString(ffff111222);
  1403. text.TextString = ylbyy4444.Substring(2, ylbyy4444.Length - 2);
  1404. break;
  1405. case "ylbyy5":
  1406. string ylbyy5555=(Convert.ToDouble(ylbstr) +2).ToString(ffff111222);
  1407. text.TextString = ylbyy5555.Substring(2, ylbyy5555.Length - 2);
  1408. break;
  1409. //case "":
  1410. // break;
  1411. //case "":
  1412. // break;
  1413. //case "":
  1414. // break;
  1415. default: break;
  1416. }
  1417. #endregion
  1418. myentity = text;
  1419. }
  1420. myentity.TransformBy(mt3d);
  1421. }
  1422. btrenu.Dispose();
  1423. btr.Dispose();
  1424. }
  1425. bt.Dispose();
  1426. myTran.Commit();
  1427. myTran.Dispose();
  1428. }
  1429. catch (Exception ex)
  1430. {
  1431. MessageBox.Show(ex.ToString());
  1432. }
  1433. }
  1434. #endregion
  1435. ////保存为dwg code
  1436. var resultfilename = dwgfilename;
  1437. ////var resultfilename = @"D:\test\result\1000tk" + i.ToString() + ".dwg";
  1438. myDB.SaveAs(resultfilename, DwgVersion.Current);
  1439. //myDB.CloseInput(true);
  1440. myDB.Dispose();
  1441. }
  1442. }
  1443. }
  1444. }
  1445. catch (Exception ex0)
  1446. {
  1447. MessageBox.Show(ex0.ToString());
  1448. }
  1449. //}
  1450. //else
  1451. // return;
  1452. }
  1453. public void InsertDOM()
  1454. {
  1455. string[] filelist;
  1456. string domfilename = string.Empty;
  1457. string tfwfilename = string.Empty;
  1458. string imageName = string.Empty;
  1459. System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
  1460. ofd.Title = "请选择要插入的DOM(必须有相应的.tfw文件且在同一文件夹中)";
  1461. ofd.Filter = "(DOM)*tif|*.tif";
  1462. ofd.Multiselect = true;
  1463. ofd.RestoreDirectory = true;
  1464. if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  1465. {
  1466. filelist = ofd.FileNames;
  1467. Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  1468. Database db = doc.Database;
  1469. Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  1470. ObjectIdCollection obidcoll = new ObjectIdCollection();
  1471. for (int i = 0; i < filelist.Length; i++)
  1472. {
  1473. domfilename = filelist[i];
  1474. imageName = new FileInfo(domfilename).Name;
  1475. if (domfilename.Contains("tif"))
  1476. tfwfilename = domfilename.Replace(".tif", ".tfw");
  1477. else
  1478. {
  1479. tfwfilename = domfilename.Replace(".TIF", ".tfw");
  1480. }
  1481. double scale = 0;
  1482. double X = 0, Y = 0;
  1483. try
  1484. {
  1485. StreamReader sr = new StreamReader(tfwfilename);
  1486. scale = Convert.ToDouble(sr.ReadLine());
  1487. sr.ReadLine();
  1488. sr.ReadLine();
  1489. sr.ReadLine();
  1490. X = Convert.ToDouble(sr.ReadLine());
  1491. Y = Convert.ToDouble(sr.ReadLine());
  1492. sr.Close();
  1493. sr.Dispose();
  1494. }
  1495. catch (System.Exception ex)
  1496. {
  1497. //MessageBox.Show("警告:"+domfilename +"图像没有相应的.tfw文件,无法正确插入!!!\r\n\r\n" + ex.ToString());
  1498. MessageBox.Show("警告:" + domfilename + "图像没有相应的.tfw文件,无法正确插入!!!\r\n\r\n");
  1499. continue;
  1500. }
  1501. using (Transaction tran = db.TransactionManager.StartTransaction())
  1502. {
  1503. LayerTable lt = tran.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
  1504. try
  1505. {
  1506. if (!lt.Has("DOM"))
  1507. {
  1508. LayerTableRecord ltrec = new LayerTableRecord();
  1509. ltrec.Name = "DOM";
  1510. ObjectId obid = lt.Add(ltrec);
  1511. tran.AddNewlyCreatedDBObject(ltrec, true);
  1512. //db.Clayer = obid;
  1513. }
  1514. }
  1515. catch (Autodesk.AutoCAD.Runtime.Exception ex)
  1516. {
  1517. MessageBox.Show(ex.ToString());
  1518. }
  1519. tran.Commit();
  1520. }
  1521. using (Transaction tran = db.TransactionManager.StartTransaction())
  1522. {
  1523. BlockTable blktbl = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  1524. BlockTableRecord blktblrec = tran.GetObject(blktbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  1525. DrawOrderTable dot = tran.GetObject(blktblrec.DrawOrderTableId, OpenMode.ForWrite) as DrawOrderTable;
  1526. try
  1527. {
  1528. #region
  1529. //RasterImageDef dom = new RasterImageDef();
  1530. ////domimage.TransformBy();
  1531. //dom.ActiveFileName = domfilename;
  1532. ////dom.OpenImage();
  1533. //dom.Load();
  1534. //dom.ImageModified = true;
  1535. //dom.OpenImage();
  1536. //dom.Embed();
  1537. //RasterImage domimage = new RasterImage();
  1538. ////Matrix3d.Mirroring();//将栅格图像坐标关于Y轴镜像还原
  1539. ////Matrix3d.Displacement();//在Y轴正方向上移动图像原始高度距离
  1540. ////domimage.TransformBy();//最后用6参数组织缩放、旋转、移动的变换矩阵
  1541. //tran.AddNewlyCreatedDBObject(dom, true);
  1542. //tran.Commit();
  1543. ///////////////////
  1544. //RasterImage domimage = new RasterImage();
  1545. //PromptEntityOptions prent = new PromptEntityOptions("\n请选择山歌图像");
  1546. //prent.SetRejectMessage("\n选择的不是栅格图像");
  1547. //prent.AddAllowedClass(typeof(RasterImage), true);
  1548. //PromptEntityResult prentres = ed.GetEntity(prent);
  1549. //if (prentres.Status != PromptStatus.OK)
  1550. // return;
  1551. //Matrix3d transform;
  1552. //ObjectId imgid = prentres.ObjectId;
  1553. //domimage = (RasterImage)tran.GetObject(imgid, OpenMode.ForRead);
  1554. //transform = domimage.PixelToModelTransform;
  1555. #endregion
  1556. //////////////////////创建图像定义对象
  1557. ObjectId imgid = RasterImageDef.GetImageDictionary(db);
  1558. if (imgid == ObjectId.Null)
  1559. imgid = RasterImageDef.CreateImageDictionary(db);
  1560. DBDictionary imgdict = tran.GetObject(imgid, OpenMode.ForWrite) as DBDictionary;
  1561. RasterImageDef imgdef = new RasterImageDef();
  1562. imgdef.SourceFileName = domfilename;
  1563. imgdef.Load();
  1564. ObjectId imgdefid = imgdict.SetAt(imageName, imgdef);
  1565. tran.AddNewlyCreatedDBObject(imgdef, true);
  1566. RasterImage image = new RasterImage();
  1567. image.ImageDefId = imgdefid;
  1568. image.Layer = "DOM";
  1569. blktblrec.AppendEntity(image);
  1570. tran.AddNewlyCreatedDBObject(image, true);
  1571. double height = imgdef.Size.Y;
  1572. Vector3d vt1 = scale * image.Orientation.Xaxis;
  1573. Vector3d vt2 = scale * image.Orientation.Yaxis;
  1574. image.AssociateRasterDef(imgdef);
  1575. image.Draw();
  1576. image.Orientation = new CoordinateSystem3d(new Point3d(X, Y - height * scale, 0), vt1, vt2);
  1577. //影像后置
  1578. obidcoll.Add(image.ObjectId);
  1579. dot.MoveToBottom(obidcoll);
  1580. obidcoll = new ObjectIdCollection();
  1581. tran.Commit();
  1582. Autodesk.AutoCAD.ApplicationServices.Application.UpdateScreen();
  1583. doc.Editor.WriteMessage(imageName + "影像已按照参数正确加载;");
  1584. }
  1585. catch (Autodesk.AutoCAD.Runtime.Exception ex)
  1586. {
  1587. MessageBox.Show(ex.ToString());
  1588. }
  1589. }
  1590. }
  1591. }
  1592. }
  1593. public List<double> getxyscale(string filename)
  1594. {
  1595. List<double> result = new List<double>();
  1596. try
  1597. {
  1598. StreamReader sr = new StreamReader(filename);
  1599. double scale = Convert.ToDouble(sr.ReadLine());
  1600. sr.ReadLine();
  1601. sr.ReadLine();
  1602. sr.ReadLine();
  1603. double X = Convert.ToDouble(sr.ReadLine());
  1604. double Y = Convert.ToDouble(sr.ReadLine());
  1605. sr.Close();
  1606. sr.Dispose();
  1607. result.Add(X);
  1608. result.Add(Y);
  1609. result.Add(scale);
  1610. return result;
  1611. }
  1612. catch
  1613. {
  1614. return new List<double>();
  1615. }
  1616. }
  1617. public void InserDOMbatch2021()
  1618. {
  1619. string dompath1 = "";
  1620. string dwgpath2 = "";
  1621. string strdomtype = ".jpg";
  1622. string strdomxyztype = ".jgw";
  1623. FolderBrowserDialog domfbd1 = new FolderBrowserDialog();
  1624. domfbd1.Description = "select dom filepath 111";
  1625. FolderBrowserDialog dwgfbd2 = new FolderBrowserDialog();
  1626. dwgfbd2.Description = "select dwg filepath 222";
  1627. if (domfbd1.ShowDialog() == DialogResult.OK)
  1628. {
  1629. dompath1 = domfbd1.SelectedPath;
  1630. }
  1631. if (dwgfbd2.ShowDialog() == DialogResult.OK)
  1632. {
  1633. dwgpath2 = dwgfbd2.SelectedPath;
  1634. }
  1635. DirectoryInfo di = new DirectoryInfo(dwgpath2);
  1636. foreach (FileInfo fi in di.GetFiles())
  1637. {
  1638. string filenamedwg = fi.FullName;
  1639. string namedwg = fi.Name;
  1640. string tfhname = namedwg.Replace(".dwg", "");
  1641. //MessageBox.Show(filenamedwg);
  1642. if (filenamedwg.Contains(".dwg"))
  1643. {
  1644. //MessageBox.Show(filenamedwg);
  1645. string filenamedom = dompath1 + "\\" + tfhname + strdomtype;
  1646. string filenamedomxyz = dompath1 + "\\" + tfhname + strdomxyztype;
  1647. string savefile = dompath1 + "\\" + tfhname + ".dwg";
  1648. //MessageBox.Show("1 "+filenamedom);
  1649. //MessageBox.Show("2 "+filenamedomxyz);
  1650. if (File.Exists(filenamedom))
  1651. {
  1652. //MessageBox.Show(filenamedom);
  1653. }
  1654. else
  1655. {
  1656. MessageBox.Show("not " + filenamedom);
  1657. continue;
  1658. }
  1659. if (File.Exists(filenamedomxyz))
  1660. {
  1661. //MessageBox.Show(filenamedomxyz);
  1662. }
  1663. else
  1664. {
  1665. MessageBox.Show("not " + filenamedomxyz);
  1666. continue;
  1667. }
  1668. List<double> xyslist = new List<double>();
  1669. xyslist = getxyscale(filenamedomxyz);
  1670. //MessageBox.Show(xyslist.Count.ToString());
  1671. double X =xyslist[0] ;
  1672. double Y=xyslist[1];
  1673. double scale=xyslist[2];
  1674. if (xyslist.Count < 2)
  1675. continue;
  1676. //MessageBox.Show(X.ToString()+Y.ToString()+scale.ToString());
  1677. ObjectIdCollection obidcoll = new ObjectIdCollection();
  1678. Database db = new Database(false, true);
  1679. db.ReadDwgFile(filenamedwg, System.IO.FileShare.ReadWrite, false, "");
  1680. db.CloseInput(true);
  1681. using (var tran = db.TransactionManager.StartTransaction())
  1682. {
  1683. try
  1684. {
  1685. //var bt = (BlockTable)(myTran.GetObject(myDB.BlockTableId, OpenMode.ForWrite));
  1686. LayerTable lt = tran.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
  1687. if (!lt.Has("DOM"))
  1688. {
  1689. LayerTableRecord ltrec = new LayerTableRecord();
  1690. ltrec.Name = "DOM";
  1691. ObjectId obid = lt.Add(ltrec);
  1692. tran.AddNewlyCreatedDBObject(ltrec, true);
  1693. //db.Clayer = obid;
  1694. //MessageBox.Show("111");
  1695. }
  1696. //MessageBox.Show("222");
  1697. }
  1698. catch
  1699. {
  1700. MessageBox.Show("DOM layer created failed");
  1701. }
  1702. }
  1703. using (var tran = db.TransactionManager.StartTransaction())
  1704. {
  1705. try
  1706. {
  1707. //MessageBox.Show("333");
  1708. BlockTable blktbl = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  1709. BlockTableRecord blktblrec = tran.GetObject(blktbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  1710. DrawOrderTable dot = tran.GetObject(blktblrec.DrawOrderTableId, OpenMode.ForWrite) as DrawOrderTable;
  1711. //////////////////////创建图像定义对象
  1712. ObjectId imgid = RasterImageDef.GetImageDictionary(db);
  1713. if (imgid == ObjectId.Null)
  1714. imgid = RasterImageDef.CreateImageDictionary(db);
  1715. DBDictionary imgdict = tran.GetObject(imgid, OpenMode.ForWrite) as DBDictionary;
  1716. RasterImageDef imgdef = new RasterImageDef();
  1717. imgdef.SourceFileName = filenamedom;
  1718. imgdef.Load();
  1719. ObjectId imgdefid = imgdict.SetAt(tfhname, imgdef);
  1720. tran.AddNewlyCreatedDBObject(imgdef, true);
  1721. //MessageBox.Show("444");
  1722. RasterImage image = new RasterImage();
  1723. image.ImageDefId = imgdefid;
  1724. //image.Layer = "DOM";
  1725. blktblrec.AppendEntity(image);
  1726. tran.AddNewlyCreatedDBObject(image, true);
  1727. //MessageBox.Show("555");
  1728. double height = imgdef.Size.Y;
  1729. Vector3d vt1 = scale * image.Orientation.Xaxis;
  1730. Vector3d vt2 = scale * image.Orientation.Yaxis;
  1731. //MessageBox.Show(vt1.X.ToString() + ',' + vt1.Y.ToString());
  1732. //MessageBox.Show(vt2.X.ToString() + ',' + vt2.Y.ToString());
  1733. image.AssociateRasterDef(imgdef);
  1734. //image.Draw();
  1735. //MessageBox.Show(X.ToString() + "+++" + (Y - height * scale).ToString());
  1736. //////////////
  1737. /////////////////
  1738. //dhkjahhdflhkflhkflkflkflkjfflhkjflhkflhkflhk
  1739. ///////////
  1740. image.Orientation = new CoordinateSystem3d(new Point3d(X, Y - height * scale, 0), vt1, vt2);
  1741. //image.Orientation = new CoordinateSystem3d(new Point3d(X, Y , 0), vt1, vt2);
  1742. //MessageBox.Show("112");
  1743. //影像后置
  1744. obidcoll.Add(image.ObjectId);
  1745. dot.MoveToBottom(obidcoll);
  1746. obidcoll = new ObjectIdCollection();
  1747. //MessageBox.Show("555");
  1748. tran.Commit();
  1749. //MessageBox.Show("777");
  1750. //Autodesk.AutoCAD.ApplicationServices.Application.UpdateScreen();
  1751. //doc.Editor.WriteMessage(imageName + "影像已按照参数正确加载;");
  1752. }
  1753. catch(Exception ex)
  1754. {
  1755. MessageBox.Show(ex.ToString());
  1756. }
  1757. }
  1758. //////////////
  1759. //MessageBox.Show("666");
  1760. //db.Save();
  1761. db.SaveAs(savefile, DwgVersion.Current);
  1762. db.Dispose();
  1763. }
  1764. }
  1765. }
  1766. ///end
  1767. ///
  1768. //////////////////////////////////////////////////////////
  1769. }
  1770. }