Create a text block

I need to create a script that does the following:

Check if a text block having the name “Costing” already exists

If true
delete the text and replace with new text
else
create the text block with the new text

Quick questions:

Are you looking for text blocks with the text “Costing” in them, or text blocks that have an object name of “Costing”?
Also, what about text blocks that are not selectable - hidden, on off layers, locked, etc. - change them as well?
Do you want to be able to specify the new text each time the script runs or is it always the same text that replaces “Costing”?

–Mitch

let me explain the scenario: I have written a routine that calculates the costing for my laser cutting. The routine currently displays all the costing information in a messagebox at the end of its run.I would like that information to be written to a text block inside the file. I don’t want a new block to be created each time I run the routine, I would rather have the text over written if the block already exists. I need to be able to recognize the block as the costing block because there might be lots of other blocks in the file.

In summary:

look for text blocks
//-------------
if a block named “costing” is found
select it
clear its text
else
ask for a x,y entry point
create a new text block
select it

Load the text into the select text block
//---------------

Once that works I plan to go on and add the ability to manage a number of costing blocks. The app will then come up with a list of existing block names and an option to add another. Depending on the choice it would then alter or create the text. The reason for wanting to add this is that I may have lots of parts that are individually costed within the file.

Ok, so once all that is sorted I want to be able to print the costing in a nicely formatted quotation. The best would probably to stuff the text into a Word template or something. (I can’t say that I have given this part much thought yet) There is just so much one can do with this idea; one could pick the customer data up from an Access database and the info can then be printed to PDF and even emailed straight away.

I’m not sure if this will help, but this snippet searches for all text containing “Costing” and deletes it. For every text object that doesn’t contain “Costing,” it adds new text with a user-specified location. I don’t think it’s exactly what you’re after, but perhaps it will help.
/SPM

import rhinoscriptsyntax as rs
import Rhino as rc

objs = rc.RhinoDoc.ActiveDoc.Objects

for obj in objs:
    if type(obj) is rc.DocObjects.TextObject:
        if "Costing" in obj.DisplayText:
            rs.DeleteObject(obj)
            print "Yay, text containing 'Costing' was deleted!"
        else:
            new_text = "This string replaces strings with 'Costing' in it."
            location = rs.GetPoint("Select a location for a new text object.")
            rs.AddText(new_text, location, height = 100)

txt_ops.py (528 Bytes)

Perfect, thank you. That is exactly what I am looking for.

1 Like