dilan Posted April 6 Posted April 6 Hi, I'm writing a function in C#. The goal is to read an XML file and extract all point numbers into a list. I can't figure out how to pass the file path for reading through an AutoCAD variable. Right now, I'm doing it via a configuration file, but that's not convenient. I want to get the file path from an AutoCAD variable—for example, my file path is stored in a variable like filepathXML. Additionally, the function currently outputs the result to the AutoCAD command line, but I need to assign this result to an AutoCAD variable (e.g., listPointID). I also can't understand how to do that. Overall, the code works and does what I need, but I'm struggling with handling the variables properly. using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Text; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.EditorInput; public class PointIdExtractor { [CommandMethod("GetPointIds")] public static void GetPointIdsCommand() { Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; try { // Getting the path from the configuration file string filePath = GetFilePathFromConfig(); if (string.IsNullOrEmpty(filePath)) { ed.WriteMessage("\nError: Failed to get file path from configuration"); return; } if (!File.Exists(filePath)) { ed.WriteMessage($"\nError: File not found at path: {filePath}"); return; } List<int> ids = GetPointIdsFromFile(filePath); // We format the output as (1 2 3 4 5 ...) ed.WriteMessage($"\nFound point IDs: {FormatAsLispList(ids)}"); } catch (System.Exception ex) { ed.WriteMessage($"\nError: {ex.Message}"); } } private static string GetFilePathFromConfig() { try { // Path to configuration file next to DLL string configPath = Path.Combine( Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "PointExtractor.cfg"); if (!File.Exists(configPath)) { // Create a file with an example if it does not exist File.WriteAllText(configPath, "DataXmlPath=C:\\path\\to\\your\\file.xml"); return null; } // Read all configuration lines foreach (string line in File.ReadAllLines(configPath)) { if (line.StartsWith("DataXmlPath=")) { return line.Substring("DataXmlPath=".Length).Trim(); } } return null; } catch { return null; } } private static string FormatAsLispList(List<int> numbers) { StringBuilder sb = new StringBuilder("("); bool first = true; foreach (int num in numbers) { if (!first) sb.Append(" "); sb.Append(num); first = false; } sb.Append(")"); return sb.ToString(); } public static List<int> GetPointIdsFromFile(string filePath) { List<int> ids = new List<int>(); const string searchPattern = "<P id=\""; try { using (StreamReader reader = new StreamReader(filePath, Encoding.UTF8)) { string line; while ((line = reader.ReadLine()) != null) { int pos = 0; while ((pos = line.IndexOf(searchPattern, pos, StringComparison.Ordinal)) != -1) { int start = pos + searchPattern.Length; int end = line.IndexOf('"', start); if (end > start) { string numStr = line.Substring(start, end - start); if (int.TryParse(numStr, out int num)) { ids.Add(num); } } pos = end > 0 ? end : pos + 1; } } } return ids; } catch (System.Exception ex) { throw new System.Exception($"Failed to process file {filePath}: {ex.Message}"); } } } Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.