NET start > .NET newbies
Hi. reciving location info about objects
(1/1)
schaap:
hi,
i want to write a command in C# that alow user to do 2 things:
1. choose a layer to work on
2. make a list/table of all objects in that layer, and their X Y coordinates.
for example - if a DWG file contain few layers, one of them has names (and only names). there are ~1500 objacts (text type) in that layer. every objact have a string (100,101,...,1350,1351...) and locating (X and Y). I want to be able to pick this layer, then bild a text file like this: (name X Y)
100 20456 40789
101 20344 40348
102 21034 40456
.
.
.
its dont has to be sorted. how do I do it? can you give me the solution or any lift to proggress?
thanks :)
schaap.
(gile):
A way to this is:
- make a selection set (Editor.SelectAll() method) with a SelectionFilter to get all DBText objects on the specified layer.
- instanciate a System.IO.StreamWriter with the file you want to create.
- within a Transaction, open every seleted object and write a line (WriteLine() method) in the file with the text TextString and the text Position.X and Position.Y.
Post what you've written yet so that it'll be easier to help you.
schaap:
hi, thanks for helping.
I'm a newbi, Dont really know where to start with your link.
I found the EATTEXT command witch gives me what I need, but in a manual way (export to excel, then sorting and filtering...). I want to write a script that does it, so I'll have a data structure thats holds all that data' for more calculations and proccesing.
were do I start?
:grandsourire:
fixo:
Try this code
--- Code: --- [CommandMethod("txtdata", CommandFlags.Modal | CommandFlags.UsePickSet)]
public void TextDataExtraction()
{
StringBuilder sb = new StringBuilder();
List<string[]> data = new List<string[]>();
// get active drawing
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
// get document editor
Editor ed = doc.Editor;
// build selection filter , put layers you need, separated by commas
//SelectionFilter sfilter= new SelectionFilter(new TypedValue[]{new TypedValue(0,"text"),new TypedValue(8,"Layer1,Layer2,Layer999")});
// to select all text on all alyers you have to elimminate second typedvalue from filter:
SelectionFilter sfilter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "text") });
// request for objects to be selected in the whole drawing
PromptSelectionResult res = ed.SelectAll(sfilter);
try
{
//check on valid selection result
if (res.Status == PromptStatus.OK)
{
//get object transaction
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
foreach (ObjectId id in res.Value.GetObjectIds())
{
Entity ent = tr.GetObject(id, OpenMode.ForRead, false) as Entity;
DBText txt = ent as DBText;
if (txt != null)
{
//if text has an empty string it will be replaced with "---"
string record = string.Format("{0}\t{1:f2}\t{2:f2}\n", txt.TextString == "" ? "---" : txt.TextString, txt.Position.X, txt.Position.Y);
//add record to string builder
sb.Append(record);
string[] item = new string[3];
item[0] = txt.TextString == "" ? "---" : txt.TextString;//<--- if text has an empty string it will be replaced with "---"
item[1] = string.Format("{0:f3}", txt.Position.X);//<--- precision 3 decimals
item[2] = string.Format("{0:f3}", txt.Position.Y);//<--- precision 3 decimals
data.Add(item);
}
}
//write data to CSV file
using (StreamWriter sw = new StreamWriter(@"C:\Test\TextRecords.csv")) //<---change file name here
{
foreach ( string[] record in data)
{
// create tab delimited string
string line = string.Format("{0}\t{1:f2}\t{2:f2}", record[0], record[1], record[2]);
sw.WriteLine(line);
}
sw.Close();
}
tr.Commit();
}
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage("\n" + ex.Message + "\n" + ex.Source + "\n" + ex.StackTrace);
}
}
--- End code ---
Navigation
[0] Message Index
Go to full version