reclosedev / pyautocad

AutoCAD Automation for Python ⛺
http://pypi.python.org/pypi/pyautocad/
BSD 2-Clause "Simplified" License
493 stars 142 forks source link

why my script doesn't iterate all text objects #41

Open Civil-3D opened 2 years ago

Civil-3D commented 2 years ago

Hello, I have a problem, I wrote some script and I want to iterate all the text in my model space and redefine changed texts.

Some texts are not iterated, Some texts iterated two times and rest of them are ok.

you can take script also, I have something like 2000 object for the testing purpose, they are all the same I just want to use this script in the project, I have to renumber chainages and add all of them 0.51m. how can I fix this problem? I think it is a pyautocad problem, every time when you just iterate texts by regular expression search it skips some text, but only cases when you use obj.Delete() or obj.Move() functions, if you just ask to show all object it will show you all of them, but if you try to change position or delete them it will skip some objects, I think some operations happens quicker then others.

`from pyautocad import Autocad, APoint

import re

acad = Autocad() pattern = re.compile("([0-9]+)+([0-9]+).([0-9]+)") # Example 37+45.63

addValue = 51 # Value To add a Chainage 0.51m

for text in acad.iter_objects('Text',limit = None):

if pattern.search(text.TextString):  #Check only Texts where patter has been found
    textContent = text.TextString    # Get full Text content for reuse

    getCoord = text.InsertionPoint #Get Existing Text Coordinates
    coord = APoint(getCoord)   #Set Coordinate for new Text
    rotation = text.Rotation # Get existing Text Rotation
    height = text.Height    # Get existing Text Height

    finded = pattern.search(textContent)# Finded Pattern Text
    a = int(finded.group(1)) # Regex Group 1 example (37)+45.63
    b = int(finded.group(2)) # Regex Group 2 example  37+(45).63
    c = int(finded.group(3)) # Regex Group 3 example  37+45.(63)

    ###################################################
    sum1 = c + addValue   #######
    if sum1 >= 100:       #######
        c = sum1 - 100    #######
        b += 1            #######
        if b + 1 >= 100:  ####### This section must be 
            a += 1        ####### A seperate Function But 
            b -= 100      ####### I did not manage
    else:                 #######
        c = sum1          #######
    ########################################################  

    modStr = pattern.sub(f'{a}+{b}.{c}', textContent) # Modified Text To be Placed
    text.Delete()   #Delete text to reinsert

    newText = acad.model.AddText(modStr, coord, height) #Create New text objects in the model
    newText.rotation = rotation # Add same rotations as source before deleting

print("Done!")`