<?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:
こんにちは、良い一日を
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>

Leave a Reply