Working with Unicode Text: Reading from and Writing to Files

<?xml encoding=”UTF-8″>By Madhukar Moogala

Before AutoCAD 2021, AutoLisp/VLisp was not fully Unicode compliant.

AutoCAD 2021 introduced a new system variable,
LISPSYS,
which fully supports AutoLisp functions with Unicode
characters.

Here’s how to read a text file with Unicode characters and create an MTEXT entity with the parsed contents:

Sample text file with Unicode characters:

&#12371;&#12435;&#12395;&#12385;&#12399;&#12289;&#33391;&#12356;&#19968;&#26085;&#12434;

The following AutoLisp code reads the text file and creates an MTEXT entity

  (defun c:drawtext()
(setq f (open "d:/temp/r.txt" "r" "utf8"))
(setq mytext (read-line f))
(setq mypoint (list 1.0 1.0 0.0)
myotherpoint (list -0.5 2.0 0.0)
mycolor "2" ; red
myrot 90.0
currcolor (getvar "CECOLOR")
)
(setvar "CECOLOR" mycolor)
(command "mtext" mypoint "R" myrot "@" mytext "")
(setvar "CECOLOR" currcolor)
(close f)
)

Note:We use utf8encoding, which tells AutoLISP to open a Unicode stream for reading and
writing.

Now, similarly, we can write the Unicode contents of an MTEXT entity to a file:

  (defun c:writetext()
(setq f (open "d:/temp/w.txt" "w" "utf8"))
(setq txt (vla-get-textstring (vlax-ename->vla-object (car (entsel "Pick mtext")))))
(write-line txt f)
(close f)
)

Here’s the result:

<a href="https://adndevblog.typepad.com/.a/6a0167607c2431970b02c8d3b0ddbc200c-pi"><img width="157" height="356" title="image" alt="image" src="http://blog.autodesk.io/wp-content/uploads/2024/05/mt_imported_image_1759122855.jpg" border="0"></a>

Comments

One response to “Working with Unicode Text: Reading from and Writing to Files”

  1. ОНЛАЙН БІБЛІОТЕКА ЕЛЕКТРОННИХ КНИГ https://ukrbook.org/

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading