ezdxf icon indicating copy to clipboard operation
ezdxf copied to clipboard

set_mtext_content() doesn't work for "annotative" MULTILEADER

Open nullplus opened this issue 2 years ago • 9 comments
trafficstars

Describe the bug I want to replace text in MultiLeader. I get text by get_mtext_content() is ok, but i can't changging for MText in MultiLeader.

my script

##################################################
# IMPORT
import sys

import ezdxf
from ezdxf.entities.text import Text
from ezdxf.entities.mtext import MText
from ezdxf.entities.mleader import MultiLeader

##################################################
# MAIN
fromString = "TESTSTRING"
toString = "@@@"
dxf = ezdxf.readfile("test.dxf")

for entity in dxf.entities:
    if type(entity) is Text:
        entity.dxf.text = entity.plain_text().replace(fromString, toString)
    if type(entity) is MText:
        entity.text = entity.plain_text().replace(fromString, toString)
    if type(entity) is MultiLeader:
        ## I want replace and write text in MultiLeader. ##
        # Not working.
        entity.set_mtext_content( entity.get_mtext_content().replace(fromString, toString) ) 

dxf.saveas("out.dxf")

Expected behavior I thought this code would allow me to replace text in text, mtext, and mleader in dxf. But does not work "set_mtext_content()" for mleader.

nullplus avatar Feb 24 '23 23:02 nullplus

Use isinstance(entity, MultiLeader) instead of type(entity) == MultiLeader, because your implementation doesn't process the MLEADER synonym.

Check for MTEXT content entity.has_mtext_content, the entity may have BLOCK content.

mozman avatar Feb 25 '23 04:02 mozman

Thank you reply.

I try, then the entity is not have BLOCK. But I Can't write by set_mtext_content().

I try change simple code. But not working.

if isinstance(entity, MultiLeader):
    entity.set_mtext_content("@@@")

Is there anything else I could try?

nullplus avatar Feb 26 '23 01:02 nullplus

Run this script:

from __future__ import annotations
import pathlib
import ezdxf
from ezdxf.math import Vec2

CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
if not CWD.exists():
    CWD = pathlib.Path(".")


def quick_mtext_horizontal():
    doc = ezdxf.new(setup=True)
    mleaderstyle = doc.mleader_styles.duplicate_entry("Standard", "EZDXF")
    mleaderstyle.set_mtext_style("OpenSans")
    msp = doc.modelspace()

    ml_builder = msp.add_multileader_mtext("EZDXF")
    ml_builder.quick_leader(
        "Line1\nLine2",
        target=Vec2(40, 15),
        segment1=Vec2.from_deg_angle(45, 14),
    )

    mleader = msp.query("MULTILEADER").first
    mleader.set_mtext_content("New Content")
    doc.set_modelspace_vport(60, center=(10, 5))
    doc.saveas(CWD / "mleader.dxf")


if __name__ == '__main__':
    quick_mtext_horizontal()

The result should look like that in your DXF viewer:

image

mozman avatar Feb 26 '23 06:02 mozman

Thank you. I try and get same result dxf.

I understand that set_mtext_content() works in the situation of creating a new mleader ;)

The next step I want to do is to make changes to the mtext in an already existing mleader. Can you give me an example script on how to make changes to an already existing mleader?

Best regards,

nullplus avatar Feb 26 '23 11:02 nullplus

The changing part works the same way for new and existing MULTILEADER, but you have to know where they are located: the modelspace, a paperspace or a block definition. This depends on your input file.

mozman avatar Feb 27 '23 05:02 mozman

I created a simple test file and tried it, and confirmed that the changes could be applied to an existing MULTILEADER.

However, it could not be applied to the file used in the first job I tried.

It seems that set_mtext_content() is not executed for MULTILEADER with 'Annonative' set. What can i do? The get_mtext_content() works fine.

nullplus avatar Mar 13 '23 21:03 nullplus

Please show an example including code and (zipped) DXF data.

mozman avatar Mar 14 '23 05:03 mozman

Thank you.

This is my script code.

#!/usr/bin/env python3
# usage: dxfrep <from> <to> <file> 
##################################################
# IMPORT
import sys
import ezdxf
from ezdxf.entities.text import Text
from ezdxf.entities.mtext import MText
from ezdxf.entities.mleader import MultiLeader

##################################################
# MAIN
fromString = sys.argv[1]
toString = sys.argv[2]
dxf = ezdxf.readfile(sys.argv[3])

for entity in dxf.entities:
    if type(entity) is Text:
        ## This is file. ##
        entity.dxf.text = entity.plain_text().replace(fromString, toString)
    if type(entity) is MText:
        ## This is file. ##
        entity.text = entity.plain_text().replace(fromString, toString)
    if isinstance(entity, MultiLeader):

        ## This is fine. ##
        print( entity.get_mtext_content().replace(fromString, toString) )

        ## Not working this for Annonative. ##
        ## I want to replace and write back text for Annonative-MultiLeader. ##
        entity.set_mtext_content( entity.get_mtext_content().replace(fromString, toString) )

dxf.saveas("out.dxf")

I run this on shell.

$ dxfrep TEST @@@ test.dxf

This is my test file what maked by AutoCAD 2023.

Best regards,

nullplus avatar Mar 14 '23 07:03 nullplus

I don't know how the annotative mode works, the MULTILEADER entity contains only the new text but BricsCAD and Trueview shows the old text even when the proxy graphic was deleted.

The only solution I found is to set annotative to False:

doc = ezdxf.readfile("test.dxf")
msp = doc.modelspace()
for e in msp.query("MULTILEADER"):
    e.set_mtext_content("EZDXF")
    e.dxf.is_annotative = False
    e.update_proxy_graphic()  # current implementation just deletes the proxy graphic

doc.saveas("out.dxf")

UPDATE: The annotative feature is very complex and not documented in the DXF reference, therefore it is not supported by ezdxf, setting ìs_annotative to False is the only supported solution.

Updating the proxy graphic is important when changing the appearance of the MULTILEADER entity in any kind, some applications e.g the drawing add-on of ezdxf show only the proxy graphic, because rendering the MULTILEADER entity is time consuming. The update method is not called automatically because in the future ezdxf may be able to create the proxy graphic and this should be done only once when the modifying process is finished.

mozman avatar Mar 14 '23 15:03 mozman