Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nullplus opened this issue Feb 24, 2023 · 9 comments
Open

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

nullplus opened this issue Feb 24, 2023 · 9 comments

Comments

@nullplus
Copy link

nullplus commented Feb 24, 2023

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.

@mozman
Copy link
Owner

mozman commented Feb 25, 2023

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.

@nullplus
Copy link
Author

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?

@mozman
Copy link
Owner

mozman commented Feb 26, 2023

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

@nullplus
Copy link
Author

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,

@mozman
Copy link
Owner

mozman commented Feb 27, 2023

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.

@nullplus
Copy link
Author

nullplus commented Mar 13, 2023

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.

@mozman
Copy link
Owner

mozman commented Mar 14, 2023

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

@nullplus
Copy link
Author

nullplus commented Mar 14, 2023

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,

@mozman
Copy link
Owner

mozman commented Mar 14, 2023

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 mozman changed the title Not working "set_mtext_content()" for MultiLeader. set_mtext_content() doesn't work for "annotative" MULTILEADER Mar 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants