Parametric Text In Mass

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
sdooweloc
Posts: 16
Joined: Thu Dec 01, 2022 4:42 pm

Parametric Text In Mass

Post by sdooweloc »

Good Day all.

I am working on learning FreeCAD and have run into something I cant quite figure out.
I have a few things modeled in OpenSCAD that I then have a script run through and change the Text variables to generate tons of STLs easily and without hassle.
I am wanting to design these things in FreeCAD to have some more options for modifying the base model easily and honestly the scripting in OpenSCAD hurts my head and I want to stop using it to make things especially as some ideas become more complex.

I have found where you can make text in Draft and then basically tie that text to a spreadsheet value and pin a point on the text to a point on a Sketch. However, I would like to have a block of text in one place left aligned and another block in another place right aligned which I cant find how to do. I also wonder if there is a way to then pass with a script or something the values I want for these text blocks rather than having to manually alter the text hundreds of times to get the set of outputs I want.

The attached model I would like to have a block of text (Probably always 3 char but cant guarantee) left aligned in the top left corner - TextLeft, and a block of text (between 1 and 5 char) right aligned in the top right Text-Right.

I have also attached an OpenSCAD similar model for comparison sake so you can see where I generally want the text and such. (I have 3 different models with similar goals of this text issue so any solution ideally could be generalized to be used in multiple applications.)

Can you please help me for if this is something I can achieve in the program fully, partially, never, etc?


OS: Arch Linux (KDE/plasma)
Word size of FreeCAD: 64-bit
Version: 0.20.31562 (Git)
Build type: Release
Branch: makepkg
Hash: d45d221edcc7a757eb4e4eb638da0db5ed2759aa
Python version: 3.10.8
Qt version: 5.15.7
Coin version: 4.0.1
OCC version: 7.6.3
Locale: C/Default (C)
Attachments
FlossDrop.FCStd
(42.17 KiB) Downloaded 18 times
floss_bobbin_labeled.scad
(1.22 KiB) Downloaded 14 times
Last edited by sdooweloc on Fri Dec 02, 2022 4:54 pm, edited 1 time in total.
User avatar
Roy_043
Veteran
Posts: 8552
Joined: Thu Dec 27, 2018 12:28 pm

Re: Parametric Text In Mass

Post by Roy_043 »

There are errors in the FlossDrop.FCStd file. I could not find or fix them, so I have created a simpler model to demonstrate the technique. Look at the Attachment Offset property of the ShapeString. Keep in mind that many fonts do not produce valid contours for padding.

When you change the text in the spreadsheet you may have to recompute the model 2x.
Attachments
shapestring_in_body.FCStd
(39.65 KiB) Downloaded 13 times
User avatar
Shalmeneser
Veteran
Posts: 9560
Joined: Wed Dec 23, 2020 12:04 am
Location: Fr

Re: Parametric Text In Mass

Post by Shalmeneser »

A recompute of the file show problem in the 1st pad :

Code: Select all

Unsupported sub-shape type
If for each feature, you open the Profile ([...] to the left) then close it immediatly, the problem is solved for this feature but propagated to the next one. :roll:
Don't know why.

v0.21.30922
sdooweloc
Posts: 16
Joined: Thu Dec 01, 2022 4:42 pm

Re: Parametric Text In Mass

Post by sdooweloc »

Interesting I get 0 errors on mine but when I go to import yours @Roy_043 I get recompute errors that there is no module named 'draftviewproviders.view_shapestring' I am betting it is because my linux machine cant parse your Windows Font File Path but thats just a hunch.

So what you shared looks like youre offsetting the Y value of the shapestring to be negative (left) by the same value as what I assume is the calculated Y length of the full string? Is that correct? So I can use +/-.Shape.BoundBox.YLength to right and left justify the text and then modify that further to place it where I want correct? Except Y is vertical so really I would need to extrapolate the same theory to X correct? IE position the text to a point or line and then offset it in X +/- for right and left justified respectively correct?
User avatar
Roy_043
Veteran
Posts: 8552
Joined: Thu Dec 27, 2018 12:28 pm

Re: Parametric Text In Mass

Post by Roy_043 »

For alignment top change the Y component of the Attachment Offset Position to:

Code: Select all

-.Shape.BoundBox.YLength
For alignment right change the X component of the Attachment Offset Position to:

Code: Select all

-.Shape.BoundBox.XLength
sdooweloc
Posts: 16
Joined: Thu Dec 01, 2022 4:42 pm

Re: Parametric Text In Mass

Post by sdooweloc »

Ahh Okay yeah that seems to do a lot of what I want it to.

So that gets the text placed where it needs to be and fixes alignment which is great!


So the next question becomes is there a way to feed it a list of values to iterate through for those pieces of text to generate a bunch of STLs based on a list of values I have?
User avatar
Roy_043
Veteran
Posts: 8552
Joined: Thu Dec 27, 2018 12:28 pm

Re: Parametric Text In Mass

Post by Roy_043 »

You can create a Python macro for that.
sdooweloc
Posts: 16
Joined: Thu Dec 01, 2022 4:42 pm

Re: Parametric Text In Mass

Post by sdooweloc »

Okay. Are there similar examples somewhere I can pull from? Coding is not my strong suit but willing to learn and figure it out.
User avatar
Roy_043
Veteran
Posts: 8552
Joined: Thu Dec 27, 2018 12:28 pm

Re: Parametric Text In Mass

Post by Roy_043 »

The code below can be a starting point:
  1. Change the file_prefix value to a folder that exists.
  2. Open the attached shapestring_in_body.FCStd file (same as the previous file, but without the spreadsheet).
  3. Copy paste the code with the correct file_prefix value in the Python console.

Code: Select all

import FreeCAD as App
import Mesh

doc = App.ActiveDocument
body = doc.getObject("Body")
shs_left = doc.getObject("ShapeString")
# shs_right = doc.getObject("ShapeString001")

file_prefix = "C:/Downloads/"

data = [["aa", "bb"],
        ["CC", "DD"],
        ["ee", "ff"]
       ]

for sub in data:
    text_left, text_right = sub
    shs_left.String = text_left
    shs_left.recompute()
    # shs_right.String = text_right
    # shs_right.recompute()
    doc.recompute()
    Mesh.export([body], file_prefix + text_left + "_" + text_right + ".stl")
Attachments
shapestring_in_body.FCStd
(24.2 KiB) Downloaded 15 times
sdooweloc
Posts: 16
Joined: Thu Dec 01, 2022 4:42 pm

Re: Parametric Text In Mass

Post by sdooweloc »

Thank you. That should be a good starting point. Guess I know what I am learning/tinkering with this weekend.
I appreciate all the help and patience.
Post Reply