commit c527abf326b4c19fc0f4d194945df31d4a498fa2 Author: facat Date: Tue May 19 21:42:45 2020 +0800 完成了核心功能。 Signed-off-by: facat diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1dbb958 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.stfolder +bin +obj +Newtonsoft.Json* +nuget.exe +ILMerge* +example +*.sln +*.suo \ No newline at end of file diff --git a/PutTowerPosition/Properties/AssemblyInfo.cs b/PutTowerPosition/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..f36dbc7 --- /dev/null +++ b/PutTowerPosition/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("NWEPDI")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("Copyright © NWEPDI 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("d70bd3b9-6432-4f15-8adf-c4a6fd9239d8")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/PutTowerPosition/Properties/Settings.Designer.cs b/PutTowerPosition/Properties/Settings.Designer.cs new file mode 100644 index 0000000..1135608 --- /dev/null +++ b/PutTowerPosition/Properties/Settings.Designer.cs @@ -0,0 +1,26 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:2.0.50727.8689 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace PutTowerPostion.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + } +} diff --git a/PutTowerPosition/Properties/Settings.settings b/PutTowerPosition/Properties/Settings.settings new file mode 100644 index 0000000..8e615f2 --- /dev/null +++ b/PutTowerPosition/Properties/Settings.settings @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/PutTowerPosition/PutTowerPosition.csproj b/PutTowerPosition/PutTowerPosition.csproj new file mode 100644 index 0000000..b3fce59 --- /dev/null +++ b/PutTowerPosition/PutTowerPosition.csproj @@ -0,0 +1,96 @@ + + + + Debug + AnyCPU + 9.0.21022 + 2.0 + {CA8FB7A3-CEB6-471C-BB97-76CBFA17293C} + Library + Properties + PutTowerPostion + PutTowerPostion + v3.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + Program + C:\Program Files\AutoCAD 2010\acad.exe + + + full + true + bin\Release\ + TRACE + prompt + 4 + true + + + + False + C:\Program Files\AutoCAD 2010\acdbmgd.dll + False + + + False + C:\Program Files\AutoCAD 2010\acmgd.dll + False + + + False + C:\Program Files (x86)\Microsoft Office\root\Office16\ADDINS\Microsoft Power Query for Excel Integrated\bin\Microsoft.Office.Interop.Excel.dll + + + False + Newtonsoft.Json.11.0.2\lib\net35\Newtonsoft.Json.dll + True + + + + 3.5 + + + + + 3.5 + + + 3.5 + + + + + + + + + + True + True + Settings.settings + + + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + + \ No newline at end of file diff --git a/PutTowerPosition/PutTowerPosition.csproj.user b/PutTowerPosition/PutTowerPosition.csproj.user new file mode 100644 index 0000000..59719fe --- /dev/null +++ b/PutTowerPosition/PutTowerPosition.csproj.user @@ -0,0 +1,8 @@ + + + false + + + ProjectFiles + + \ No newline at end of file diff --git a/PutTowerPosition/Utils.cs b/PutTowerPosition/Utils.cs new file mode 100644 index 0000000..e95872a --- /dev/null +++ b/PutTowerPosition/Utils.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace PutTowerPosition +{ + class Utils + { + static public String NormDirectoryPath(String path)//把文件路径标准化为c:/abc/c的格式 + { + String newPath = path.Trim(); + newPath = newPath.Replace('\\', '/'); + if ('/' == newPath[newPath.Length - 1]) + { + newPath = newPath.Remove(newPath.Length - 1); + } + return newPath; + } + } +} diff --git a/PutTowerPosition/cmd.cs b/PutTowerPosition/cmd.cs new file mode 100644 index 0000000..75df6f4 --- /dev/null +++ b/PutTowerPosition/cmd.cs @@ -0,0 +1,344 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Autodesk.AutoCAD.Runtime; +using Autodesk.AutoCAD.ApplicationServices; +using Autodesk.AutoCAD.DatabaseServices; +using Autodesk.AutoCAD.EditorInput; +using Autodesk.AutoCAD.Geometry; +using System.Text.RegularExpressions; +using System.Windows.Forms; +using System.Security.Cryptography; +using System.Runtime.InteropServices; +using System.IO; +using System.Reflection; + +[assembly: CommandClass(typeof(PutTowerPosition.Cmd))] +namespace PutTowerPosition +{ + + + public class Cmd + { + struct spanStruct + { + + public string towerName; + public double span; + }; + + struct Point2D + { + public double X; + public double Y; + }; + + public void PutTowerPositionCmd() + { + + } + + [CommandMethod("ptp", CommandFlags.Session)] + public static void PutTowerPosition() + { + string excelFile = findCordinationExcel(@"d:\code\PutTowerPosition\example\S000.DAT"); + Dictionary cordDic = readCordExcel(excelFile); + List spanList = readSSpan(@"d:\code\PutTowerPosition\example\S000.DAT"); + Document acDoc; + Database acCurDb; + //acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(filePath, true); + acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; + using (DocumentLock acLckDoc = acDoc.LockDocument()) + { + acCurDb = acDoc.Database; + GetOrCreateLayer("TW", acCurDb); + using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) + { + + BlockTable acBlkTbl; + acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, + + OpenMode.ForWrite) as BlockTable; + BlockTableRecord acBlkTblRec; + + acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], + + OpenMode.ForWrite) as BlockTableRecord; + + Point2D[] cordinationList = calTowerXYPostion(cordDic, spanList); + foreach (Point2D point in cordinationList) + { + DBPoint dbPoint = new DBPoint(new Point3d(point.Y, point.X, 0)); + dbPoint.Layer = "TW"; + acBlkTblRec.AppendEntity(dbPoint); + acTrans.AddNewlyCreatedDBObject(dbPoint, true); + acCurDb.Pdmode = 35; + acCurDb.Pdsize = 15; + + //break; + } + acTrans.Commit(); + } + //acBlkTblRec.Database.SaveAs(outputFilePath, false, DwgVersion.Current, acDoc.Database.SecurityParameters); + } + return; + } + + + private bool UnlockAllLayer(Document doc) + { + var acCurDb = doc.Database; + //var ed = doc.Editor; + LayerTableRecord ltr = null; + using (var acTrans = acCurDb.TransactionManager.StartTransaction()) + { + LayerTable layerTable; + + layerTable = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForWrite) as LayerTable; + + foreach (ObjectId ltrId in layerTable) + { + // Don't try to lock/unlock either the current layer or layer 0 + // (depending on whether lockZero == true for the latter) + ltr = (LayerTableRecord)acTrans.GetObject(ltrId, OpenMode.ForWrite); + ltr.IsLocked = false; + //ltr.IsOff = ltr.IsOff; // This is needed to force a graphics update + + } + acTrans.Commit(); + } + return true; + } + + + static private LayerTableRecord GetOrCreateLayer(string layerName, Database db) + { + LayerTableRecord ltr; + using (Transaction tr = db.TransactionManager.StartTransaction()) + { + + LayerTable lt = + + (LayerTable)tr.GetObject( + + db.LayerTableId, + + OpenMode.ForRead + + ); + if (lt.Has(layerName)) + { + ObjectId ltId = lt[layerName]; + ltr = (LayerTableRecord)tr.GetObject(ltId, OpenMode.ForWrite); + } + else + { + ltr = new LayerTableRecord(); + ltr.Name = layerName; + lt.UpgradeOpen(); + ObjectId ltId = lt.Add(ltr); + tr.AddNewlyCreatedDBObject(ltr, true); + + } + tr.Commit(); + } + + return ltr; + } + + + + private static ObjectIdCollection GetEntitiesOnLayer(Document acDoc, string layerName) + { + + //Document doc = + + //Application.DocumentManager.MdiActiveDocument; + + Editor ed = acDoc.Editor; + + // Build a filter list so that only entities + + // on the specified layer are selected + + TypedValue[] tvs = + + new TypedValue[1] { + + new TypedValue( + + (int)DxfCode.LayerName, + + layerName + + ) + + }; + + SelectionFilter sf = + + new SelectionFilter(tvs); + + PromptSelectionResult psr = + + ed.SelectAll(sf); + + if (psr.Status == PromptStatus.OK) + + return + + new ObjectIdCollection( + + psr.Value.GetObjectIds() + + ); + + else + + return new ObjectIdCollection(); + + } + + static string findCordinationExcel(string findDirOrFile) + { + string dir = findDirOrFile; + FileAttributes attr = File.GetAttributes(findDirOrFile); + if ((attr & FileAttributes.Directory) > 0) + { + + } + else + { + dir = Path.GetDirectoryName(findDirOrFile); + } + + string cordExcelFile = dir + "/cord.xlsx"; + return cordExcelFile; + } + static private object get_value(Microsoft.Office.Interop.Excel.Worksheet sheet, string str_rang) + { + return sheet.get_Range(str_rang, Missing.Value).Value2; + } + static Dictionary readCordExcel(string excelFilePath) + { + //TowerName X Y + Microsoft.Office.Interop.Excel.Application objApp; + Microsoft.Office.Interop.Excel._Workbook objBook; + Microsoft.Office.Interop.Excel.Worksheet w_sheet; + objApp = new Microsoft.Office.Interop.Excel.Application(); + objBook = objApp.Application.Workbooks.Add(excelFilePath); + w_sheet = objBook.Sheets[1] as Microsoft.Office.Interop.Excel.Worksheet; + Dictionary towerCordination = new Dictionary(); + for (int i = 1; i < 10000; i++) + { + string towerName = (string)(get_value(w_sheet, "A" + i.ToString())); + if (towerName == null || towerName.Trim() == "") + { + break; + } + double[] cordination = new double[2]; + cordination[0] = (double)(get_value(w_sheet, "B" + i.ToString())); + cordination[1] = (double)(get_value(w_sheet, "C" + i.ToString())); + towerCordination.Add(towerName, cordination); + } + return towerCordination; + + } + + static List readSSpan(string SFilePath) + { + StreamReader reader = new StreamReader(SFilePath, Encoding.GetEncoding("gb2312")); + string line; + double basicMileage = -1; + List spanList = new List(); + Dictionary has_tower = new Dictionary(); + while ((line = reader.ReadLine()) != null) + { + if (line.Trim().StartsWith("首端") | line.Trim().StartsWith("塔号")) + { + continue; + } + if (line.Trim() == "") + { + continue; + } + string norm_line; + norm_line = Regex.Replace(line, @"\s+", ","); + norm_line = norm_line.Substring(1); + string[] sep = norm_line.Split(','); + string towerName = sep[0]; + if (has_tower.ContainsKey(towerName)) + { + continue; + } + has_tower.Add(towerName, ' '); + double mileage = Double.Parse(sep[1]); + double span; + if (basicMileage == -1) + { + basicMileage = mileage; + } + span = mileage - basicMileage; + spanStruct _span; + _span.towerName = towerName; + _span.span = span; + spanList.Add(_span); + basicMileage = mileage; + + } + reader.Close(); + return spanList; + } + + static Point2D[] calTowerXYPostion(Dictionary cordDic, List SSpan) + { + Point2D[] cordinationList = new Point2D[SSpan.Count]; + Point2D startPoint; + int tensionStart;//耐张段第一基塔位置 + tensionStart = 0; + //find next end point + Point2D endPoint; + while (true) + { + startPoint.X = cordDic[SSpan[tensionStart].towerName][0]; + startPoint.Y = cordDic[SSpan[tensionStart].towerName][1]; + for (int i = tensionStart+1; i < SSpan.Count; i++) + { + string towerName; + towerName = SSpan[i].towerName; + if (cordDic.ContainsKey(towerName)) + { + endPoint.X = cordDic[towerName][0]; + endPoint.Y = cordDic[towerName][1]; + cordinationList[tensionStart] = startPoint; + double mileageInTension=0;//耐张段中的里程 + for (int j = tensionStart + 1; j < i; j++) + { + Point2D suspensionTowerPostion; + double tensionLength;//耐张段长度 + tensionLength = Math.Sqrt(Math.Pow(endPoint.X - startPoint.X, 2) + Math.Pow(endPoint.Y - startPoint.Y, 2)); + mileageInTension += SSpan[j].span; + suspensionTowerPostion.X = startPoint.X + (endPoint.X - startPoint.X) / tensionLength * mileageInTension; + suspensionTowerPostion.Y = startPoint.Y + (endPoint.Y - startPoint.Y) / tensionLength * mileageInTension; + cordinationList[j]=suspensionTowerPostion; + } + cordinationList[i] = endPoint; + tensionStart = i; + break; + } + } + if (tensionStart >= (SSpan.Count - 1)) + { + break; + } + } + return cordinationList; + + } + + + + + } +}