Skip to content

Commit

Permalink
added 3d text artist object and shift to center dev
Browse files Browse the repository at this point in the history
  • Loading branch information
mhelmrei committed Nov 28, 2023
1 parent f2771ec commit cdef1ea
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/compas_cadwork/artists/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from compas.artists import Artist

from compas_monosashi.sequencer import Text3d
from compas_monosashi.sequencer import Text3d_Volume
from compas_monosashi.sequencer import LinearDimension
from compas_monosashi.sequencer import Model3d

Expand All @@ -16,6 +17,8 @@
"CadworkArtist",
"Text3dInstructionArtist",
"Text3dVolumeInstructionArtist",
"LinearDimensionArtist",
"Model3dArtist"
]


Expand Down Expand Up @@ -44,6 +47,8 @@ def redraw_cadwork():

@plugin(category="factories", requires=[CONTEXT])
def register_artists():
Artist.register(Text3d, Text3dInstrcutionArtist, context=CONTEXT)
Artist.register(Text3d, Text3dInstructionArtist, context=CONTEXT)
Artist.register(Text3d_Volume, Text3dVolumeInstructionArtist, context=CONTEXT)
Artist.register(LinearDimension, LinearDimensionArtist, context=CONTEXT)
Artist.register(Model3d, Model3dArtist, context=CONTEXT)

71 changes: 68 additions & 3 deletions src/compas_cadwork/artists/instructionartist.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from element_controller import create_text_object
from element_controller import create_text_object_with_font
from element_controller import create_text_object_with_options
from element_controller import move_element
from element_controller import get_bounding_box_vertices_local
from file_controller import import_element_light
import cadwork

Expand Down Expand Up @@ -73,6 +75,68 @@ def __init__(self, text_volume_instruction: Text3d_Volume, **kwargs) -> None:
super().__init__(text_volume_instruction)
self.text_volume_instruction = text_volume_instruction

def generate_translation_vectors_from_bounding_box_local(self, element_ids: list = [],
input_geometry: str = "3d text"):

"""Generates translation vectors from a bounding box that shift a text
or a box from the bottom left point of the object to the center point
of the object.
Parameters
----------
element_ids : list
The respective texts or boxes
input_geometry : string
Type of object
Return
----------
Cadwork Vector X and Z
vx, vz
"""

bb_vl = get_bounding_box_vertices_local(element_ids[0], element_ids)

# as explained and diagrammed for some reason the bounding box vertices
# are sorted differently for a 3d text or a 3d box
keys_start_end = {
"Bounding Box": {
"start_vec_x": 0,
"end_vec_x": 3,
"start_vec_z": 3,
"end_vec_z": 2
},
"3d text": {
"start_vec_x": 5,
"end_vec_x": 6,
"start_vec_z": 6,
"end_vec_z": 3
}
}

vx = cadwork.point_3d(*bb_vl[keys_start_end[input_geometry]["start_vec_x"]]) - cadwork.point_3d(
*bb_vl[keys_start_end[input_geometry]["end_vec_x"]])
dx = cadwork.point_3d(*bb_vl[keys_start_end[input_geometry]["start_vec_x"]]).distance(
cadwork.point_3d(*bb_vl[keys_start_end[input_geometry]["end_vec_x"]]))/2

vx = vx.normalized()
vx = vx*dx*-1

vz = cadwork.point_3d(*bb_vl[keys_start_end[input_geometry]["end_vec_z"]]) - cadwork.point_3d(
*bb_vl[keys_start_end[input_geometry]["start_vec_z"]])
dz = cadwork.point_3d(*bb_vl[keys_start_end[input_geometry]["start_vec_z"]]).distance(
cadwork.point_3d(*bb_vl[keys_start_end[input_geometry]["end_vec_z"]]))/2

vz = vz.normalized()
vz = vz*dz*-1

return vx, vz

def shift_text_from_bottom_left_to_center(self, element_ids: list = []):
vx, vz = self.generate_translation_vectors_from_bounding_box_local(element_ids)
move_element(element_ids, vx + vz)

def draw(self, *args, **kwargs):
"""Adds a text element with the text included in the provided text instruction.
Expand All @@ -86,18 +150,19 @@ def draw(self, *args, **kwargs):
text_options.set_color(5)
text_options.set_element_type(cadwork.volume)
text_options.set_text(self.text_volume_instruction.text)
text_options.set_height(100.0)
text_options.set_thickness(5.0)
text_options.set_height(50.0)
text_options.set_thickness(0.5)

# font = "Times New Roman"
loc = self.text_volume_instruction.location
element_id = create_text_object_with_options(
self.text_volume_instruction.text,
point_to_cadwork(loc.point),
vector_to_cadwork(loc.xaxis),
vector_to_cadwork(loc.yaxis),
text_options
)

self.shift_text_from_bottom_left_to_center([element_id])
self.add_element(element_id)
set_user_attribute([element_id], self.USER_ATTR_NUMBER, self.USER_ATTR_VALUE)
return element_id
Expand Down

0 comments on commit cdef1ea

Please sign in to comment.