How to get DImensions Text.

426 views
Skip to first unread message

t.ogawa

unread,
May 9, 2019, 1:22:04 AM5/9/19
to python-ezdxf
Dear Manfred.

I want to get dimensions text.

The following URL wrotes "Dimension Support". How can I get it?


now use:
 Python 3.7
 ezdxf 0.9 (use pip install https://pypi.org/project/ezdxf/)


thank you.
ss.png

Manfred Moitzi

unread,
May 9, 2019, 3:27:09 AM5/9/19
to python-ezdxf
Dimension.dxf.text

Dimension text explicitly entered by the user

  • If "" or "<>", the dimension measurement is drawn as the text,
  • if “ “ (one blank space), the text is suppressed.
  • Anything else is drawn as the text.

Therefore you don't get the dimension measurement by Dimension.dxf.text.

t.ogawa

unread,
May 9, 2019, 4:18:19 AM5/9/19
to python-ezdxf
Hi Manfred.

Thank you for reply.

Sorry for the small amount of information.
I want to get dimension text in dxf file.

[sample.dxf]

.
.
.
AcDbEntity
 
8
FORMAT
100
AcDbMText
 
10
73.6378494935
 
20
113.6626139923
 
30
0.2
 
40
3.0
 
41
0.0
 
71
     
1
 
72
     
1
 
1
10x10-100x100SampleText   I want to get this text.
 
7
SLDTEXTSTYLE1
 
73
     
1
 
44
1.0
 
0
ENDBLK
 
5
196
330
194
100
AcDbEntity
 
8
0
100
AcDbBlockEnd
.
.
.


↓ I'm using this code.

for entity in dxf.entities:
    e
= {}
    e
["type"] = entity.dxftype()
   
if e["type"] == "TEXT":
        e
["insert"] = entity.dxf.insert
        e
["text"] = entity.dxf.text
   
elif e["type"] == "MTEXT":
        e
["insert"] = entity.dxf.insert
        e
["raw_text"] = entity.get_text()



But I coudn't get dimension text.

How can i get dimension text in dxf file?


thank you.

2019年5月9日木曜日 16時27分09秒 UTC+9 Manfred Moitzi:

Manfred Moitzi

unread,
May 9, 2019, 4:53:59 AM5/9/19
to python-ezdxf
This excerpt seem to be part of the anonymous dimension block, which you don't need if you are really examine DIMENSION entities:

Print all DIMENSION text values, just searching the model space:

import ezdxf

doc
= ezdxf.readfile('your_file.dxf')
msp
= doc.modelspace()
for dimension in msp.query('DIMENSION'):
   
print("{} text: {}".format(str(dimension), dimension.dxf.text)

Message has been deleted

t.ogawa

unread,
May 9, 2019, 5:45:15 AM5/9/19
to python-ezdxf
Thank you for reply.

I tried it.but I got an error.

Use code:

msp = dxf.modelspace()

for dimension in msp.query('DIMENSION'):
 
    
print("{} text: {}",format(str(dimension), dimension.dxf.text))



Result:

- - - - - - - - - -

Traceback (most recent call last):

  File "/usr/local/lib/python3.7/site-packages/ezdxf/lldxf/attributes.py", line 117, in get_attrib

    return self._get_dxf_attrib(entity.tags)

  File "/usr/local/lib/python3.7/site-packages/ezdxf/lldxf/attributes.py", line 137, in _get_dxf_attrib

    return subclass_tags.get_first_value(self.code)

  File "/usr/local/lib/python3.7/site-packages/ezdxf/lldxf/tags.py", line 134, in get_first_value

    raise DXFValueError(code)

ezdxf.lldxf.const.DXFValueError: 1


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "python_read_dxf_ez.py", line 21, in <module>

    print("{} text: {}",format(str(dimension), dimension.dxf.text))

  File "/usr/local/lib/python3.7/site-packages/ezdxf/dxfentity.py", line 41, in __getattr__

    return self._wrapper.get_dxf_attrib(attrib)

  File "/usr/local/lib/python3.7/site-packages/ezdxf/dxfentity.py", line 203, in get_dxf_attrib

    return dxfattr.get_attrib(self, key, default)

  File "/usr/local/lib/python3.7/site-packages/ezdxf/lldxf/attributes.py", line 128, in get_attrib

    raise DXFValueError("DXFAttrib '%s' does not exist." % key)

ezdxf.lldxf.const.DXFValueError: DXFAttrib 'text' does not exist.


- - - - - - - - - -



I also tried the following code:



Code1:

msp = dxf.modelspace()

for dimension in msp.query('DIMENSION'):
 
    
print("{} text: {}",format(str(dimension), dimension.dxf.text)

Code1 Result:

- - - - - - - - - -

DIMENSION(#E3)
DIMENSION(#F4)
DIMENSION(#104)
DIMENSION(#111)
DIMENSION(#11D)

- - - - - - - - - -


Code2:

msp = dxf.modelspace()

for dimension in msp.query('DIMENSION'):

    
print(str(dimension.dxf))

Code2 Result:

- - - - - - - - - -

<ezdxf.dxfentity.DXFNamespace object at 0x10d32c7c8>
<ezdxf.dxfentity.DXFNamespace object at 0x10d32c828>
<ezdxf.dxfentity.DXFNamespace object at 0x10d32c888>
<ezdxf.dxfentity.DXFNamespace object at 0x10d32c8e8>
<ezdxf.dxfentity.DXFNamespace object at 0x10d32c948>

- - - - - - - - - -


Code3:

msp = dxf.modelspace()

for dimension in msp.query('DIMENSION'):
 
    
print(str(dimension.dxf.txt))

Code3 Result:

- - - - - - - - - -

.
.
.
File "/usr/local/lib/python3.7/site-packages/ezdxf/lldxf/attributes.py", line 128, in get_attrib

    raise DXFValueError("DXFAttrib '%s' does not exist." % key)

ezdxf.lldxf.const.DXFValueError: DXFAttrib 'text' does not exist.


- - - - - - - - - -



can't use "dimension.dxf.text". Is the version of ezdxf different?

thank you.

Manfred Moitzi

unread,
May 9, 2019, 6:49:48 AM5/9/19
to python-ezdxf
It seems the text attribute is not set (present), the text attribute is not optional according to the DXF reference, but this often means nothing, because you can not rely on the DXF reference.

If AutoCAD opens your DXF drawing, an optional text attribute is ok, if not your DXF file is invalid.

To avoid such problems use the get_dxf_attrib() method, less elegant, but safe (try ... except would also work):

msp = dxf.modelspace()
for dimension in msp.query('DIMENSION'):

    text
= dimension.get_dxf_attrib('text', default=None)
   
if text is not None:
       
print(text)
Message has been deleted

t.ogawa

unread,
May 9, 2019, 11:47:06 PM5/9/19
to python-ezdxf
Hi Manfred.

Thank you for reply. 
And thanks to you, the problem is solved !

Thank you so much.

t.ogawa

t.ogawa

unread,
May 10, 2019, 3:08:38 AM5/10/19
to python-ezdxf
Hi Manfred.

Sorry to bother you again.

I got custom label text.I was able to get the text of a custom label set to Dimension.

However, the numerical value ("4.5") of the attached image  could not be acquired.
When I read a DXF file, this number looks like an MTEXT Entity.

- - - - - - - - - - - - - - - - - - - - -
  0
MTEXT
  5
110
330
106
100
AcDbEntity
  8
FORMAT
440
 16777216
100
AcDbMText
 10
163.852832464
 20
129.1995374937
 30
2.6
 40
3.0
 41
0.0
 46
0.0
 71
     1
 72
     1
  1
4.5   ← This one.
  7
SLDTEXTSTYLE1
- - - - - - - - - - - - - - - - - - - - -

But, I could not find it in the next way.


[Try1]

for entity in dxf.entities:

  if e["type"] == "MTEXT":


[Try2]

msp = dxf.modelspace()

for mtext in msp.query('MTEXT'):

    print(mtext.dxfattribs())




Is there no way to get this?


thank you.

dimension_ss.png
SampleDXF.dxf

Manfred Moitzi

unread,
May 10, 2019, 4:37:12 AM5/10/19
to python-ezdxf
MTEXT content is organized in multiple DXF tags, therefore a method MText.get_text() exists.

t.ogawa

unread,
May 10, 2019, 4:56:34 AM5/10/19
to python-ezdxf
Thank you for reply.

I'm sorry I didn't make it clear enough.  

>therefore a method MText.get_text() exists.

No, I can not find any MTEXT Entity from the attached dxf.


[Try1]
for entity in dxf.entities:
  if  
entity.dxftype == "MTEXT":
   
print("MTEXT HIT")
 
else:
   
print
entity.dxftype)

[Try1.Result]

DIMENSION(#104)

DIMENSION(#104)


[Try2]
msp = dxf.modelspace()
for mtext in msp.query('MTEXT'):
 
print(mtext.dxfattribs())


[Try2.Result]
Nothing



From the attached file, only the "DIMENSION" entity was found.

Reply all
Reply to author
Forward
0 new messages