Exort data

Exort data

s_velasquez
Advocate Advocate
501 Views
7 Replies
Message 1 of 8

Exort data

s_velasquez
Advocate
Advocate

I need to export the data obtained with (entget(car(entsel)) from AutoList to a .txt or .json file, and use this data to create the same object in C#.
Can anyone help me?
Thanks.

$ (entget (car (entsel)))
((-1 . <Entity name: 243aece7f10>) (0 . "LWPOLYLINE") (330 . <Entity name: 243aecec9f0>) (5 . "2F1") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "0") (100 . "AcDbPolyline") (90 . 8) (70 . 1) (43 . 0.0) (38 . 0.0) (39 . 0.0) (10 192.5 -84.0) (40 . 0.0) (41 . 0.0) (42 . 0.0) (91 . 0) (10 192.5 84.0) (40 . 0.0) (41 . 0.0) (42 . 0.0) (91 . 0) (10 175.0 84.0) (40 . 0.0) (41 . 0.0) (42 . 0.0) (91 . 0) (10 166.75 75.7504) (40 . 0.0) (41 . 0.0) (42 . 0.181642) (91 . 0) (10 100.0 49.0) (40 . 0.0) (41 . 0.0) (42 . 0.0) (91 . 0) (10 100.0 -49.0) (40 . 0.0) (41 . 0.0) (42 . 0.181642) (91 . 0) (10 166.75 -75.7504) (40 . 0.0) (41 . 0.0) (42 . 0.0) (91 . 0) (10 175.0 -84.0) (40 . 0.0) (41 . 0.0) (42 . 0.0) (91 . 0) (210 0.0 0.0 1.0))

0 Likes
Accepted solutions (2)
502 Views
7 Replies
Replies (7)
Message 2 of 8

_gile
Consultant
Consultant

Hi,

Mixing LISP, text or json, and C# is definitely not a simple and robust way. You could easily do this in plain C#.

Anyway, one way could be,

- on the LISP side:

(setq file (open fileName "w"))
(princ (vl-prin1-to-string
	 (vl-remove-if '(lambda (x) (= (type (cdr x)) 'ename)) (entget (car (entsel))))
       )
       file
)
(close file)

-on the C# side:

Document doc = Application.DocumentManager.MdiActiveDocument;
string data = System.IO.File.ReadAllText(fileName);
doc.SendStringToExecute($"(entmakex '{data}) ", false, false, false);

 

Note, by removing pairs from the LISP list, extension dictionary will be lost.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 8

s_velasquez
Advocate
Advocate

Thanks for the answer Gile. My intention is to build the entities with C#. Since I will be doing this with a large number of entities,

I thought about using a .json file to store the data for each one to later use with:

using (Transaction trans = db.TransactionManager.StartTransaction())
 {
     Polyline polylineTop = new Polyline(8);
...
}
0 Likes
Message 4 of 8

_gile
Consultant
Consultant
Accepted solution

Why not simply use Database.WBlockCloneObjects to copy entities from one drawing to another?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 8

ActivistInvestor
Mentor
Mentor

Can you elaborate on why you think you need to persist entities as TXT or JSON, rather than as DXF or DWG files?  It's a simple matter to write entities to a DWG file and then insert them, so unless you are intending to process the exported TXT/JSON in some way, what other purpose is there to doing that?

 

You can clone objects to a new, empty Database and then save that in DXF form, which doesn't require more than a few lines of C#, and handles complex cases where objects have extension dictionaries and extended entity data attached to them. What you are envisioning is a complete reinvention of that for the sake of (JSON?), which would require much more work to cover all cases.

Message 6 of 8

s_velasquez
Advocate
Advocate

I apologize for not expressing my objective very well. My application has a very large number of drawings to be distributed as a library.

I thought of the following situation: When the user selects a component to insert into the drawing, I check if this component already exists as a block in the drawing's database.

If it does not exist, a block definition will be created and it can then be inserted.

Since I already have these drawings, I thought of storing the information for their creation in a method that is easy to manipulate, a .json file or a database.

At first I thought of the .json file because it is easier to manage and would take less time.

I do not intend to favor this or that method. I wrote the post looking for information on the best way.

English is not my native language, which is why I have difficulty expressing myself.

0 Likes
Message 7 of 8

ActivistInvestor
Mentor
Mentor
Accepted solution

Your use case is not uncommon, but persisting AutoCAD objects in JSON or any textual format is not nearly as simple as it may at-first seem. There are in many cases,  IORs (Inter-Object Relationships), where entities may store within their data, references to other, 'related' entities, as is the case with extension dictionaries, dynamic block definitions, hatching, constrained geometry, and so on. When entities store references to other entities, the problem becomes much more complicated, and is not nearly as simple as storing simple data types like points, strings, numbers etc.

 

You'll note that @_gile's LISP example removes Entity Name elements from the list. That's because entity names do not have an external representation (you would have to convert them to entity handle strings, but only where appropriate). Then when you read the data back and try to create AutoCAD entities from it, you must restore or 'translate' the entity handles back into ObjectIds (the managed equivalent of a LISP ENAME type). Doing that is massively complicated and would require massive amounts of code, and may not always work depending on the type(s) of relationships involved.

 

Storing AutoCAD entities in DWG files requires very little code and is clearly simpler and more reliable.

0 Likes
Message 8 of 8

s_velasquez
Advocate
Advocate

I am very grateful for the suggestions and comments presented.
They will help me a lot in my choice.

0 Likes