The module pycatia must already be installed.
Access the CATIA COM object with a .CATPart open and get the center of gravity for the part body ‘PartBody’.
#! /usr/bin/python3.6
"""
Example 1:
Get the center of gravity for the part body 'PartBody'.
"""
from pycatia import catia
documents = catia.documents
documents.open(r'tests/cat_files/part_measurable.CATPart')
# get the active document
document = catia.active_document
# >>> print(document.path())
# >>> C:\Users\evereux\python\projects\pycatia\tests\CF_catia_measurable_part.CATPart
# get the Part() object.
part = document.part()
# get the Bodies() collection
bodies = part.bodies
# gets first Body()
body = bodies.item(1)
# >>> print(body)
# >>> Body(name="PartBody")
# or get the body by name
# >>> body_by_name = bodies.get_item_by_name('AnotherPartBody')
# >>> print(body)
# >>> Body(name="AnotherPartBody")
# initialise the spa workbench
spa_workbench = document.spa_workbench()
# create a reference to measure.
reference = part.create_reference_from_object(body)
measurable = spa_workbench.get_measurable(reference)
center_of_gravity = measurable.get_cog()
print(center_of_gravity)
Get all the points in the geometrical set ‘Points’ and print the co-ordinate.
#! /usr/bin/python3.6
"""
Example 2:
Get all the points in the geometrical set 'Points' and output co-ordinate to console.
Create your own CATPart with a Geometrical Set called construction_points. Add some points to the Geometrical Set.
"""
from pycatia import catia
documents = catia.documents
# this should be the path to your file.
documents.open(r'tests\cat_files\part_measurable.CATPart')
document = catia.active_document
part = document.part()
spa_workbench = document.spa_workbench()
hybrid_bodies = part.hybrid_bodies
hybrid_body = hybrid_bodies.get_item_by_name('construction_points')
shapes = hybrid_body.hybrid_shapes.items()
for point in shapes:
reference = part.create_reference_from_object(point)
measurable = spa_workbench.get_measurable(reference)
coordinates = measurable.get_point()
print(f'{point.name}: {coordinates}')
Find all points in the CATPart and print it’s co-ordinate.
#! /usr/bin/python3.6
"""
Example 3:
Find all points in the CATPart and print to console -> and export to csv.
"""
import csv
from pycatia import catia
documents = catia.documents
documents.open(r'tests/cat_files/part_measurable.CATPart')
document = catia.active_document
spa_workbench = document.spa_workbench()
part = document.part()
selected = document.search_for_items(['Point'])
# export the points to a csv file.
csv_file_name = '__junk__\\exported_points.csv'
with open(csv_file_name, 'w', newline='') as csv_file:
csv_writer = csv.writer(csv_file, delimiter=',')
for selection in selected:
reference = part.create_reference_from_object(selection)
measurable = spa_workbench.get_measurable(reference)
# print to console.
print(selection.name, measurable.get_point())
x, y, z = measurable.get_point()
csv_writer.writerow([selection.name, x, y, z])
Loop through a CATProduct and find if sub component is a CATPart or CATProduct.
#! /usr/bin/python3.6
"""
Example 4:
Loop through a CATProduct and find if sub component is a CATPart or CATProduct.
"""
from pycatia import catia
from pycatia.enumeration.enumeration_types import cat_work_mode_type
documents = catia.documents
documents.open(r'tests/cat_files/product_top.CATProduct')
document = catia.active_document
product = document.product()
# Change the work mode to Design Mode.
# This is useful for CATIA configurations that work with a cache otherwise
# methods on children may fail due to the document not being loaded.
product.apply_work_mode(cat_work_mode_type.index("DESIGN_MODE"))
products = product.get_products()
if len(products) == 0:
print("Active document has no children or is not a CATProduct.")
for item in products:
if item.is_catpart():
print(f'This is a part: "{item}"')
print('')
if item.is_catproduct():
product = item
print(f'This is a product: "{item}"')
if item.has_children():
print('This product has children.')
children = item.get_children()
print(children)
print('')
Reads a csv file containing point data and adds to the active catia part.
#! /usr/bin/python3.6
"""
Example 5:
Creates a new CATIA file.
Reads a csv file containing point data and adds to the new catia part.
Formatting of csv data should be:
<point_name>,<x coordinate>,<y coordinate>,<z coordinate>
"""
from pycatia import catia
from pycatia.scripts.csv_tools import create_points
# # disable display refreshing to try tp speed up point generation.
# catia.refresh_display = False
# # hide catia window
# catia.visible = False
documents = catia.documents
# create a new part.
documents.add('Part')
document = catia.active_document
part = document.part()
# full path name to csv file.
file = r'tests\Sample_Point_CSV_File1_small.csv'
# create the points.
create_points(part, file, units='mm', geometry_set_name='Points_Construction')
# if you can't see the points hide your origin planes and refram window.
# # re-enable display refresh
# catia.refresh_display = True
# # unhide catia window
# catia.visible = True
Open a catia file and close a catia file.
#! /usr/bin/python3.6
"""
Example 6:
Open a catia file.
Export catia file to igs.
Close a catia file.
"""
from pathlib import Path
import os
from pycatia import catia
# path to file to open.
file_name = r'tests\cat_files\part_measurable.CATPart'
# open document
documents = catia.documents
documents.open(file_name)
document = catia.active_document
# _Full_ path of new file. This uses current working directory.
new_file_name = Path(os.getcwd(), 'new_part.CATPart')
# save document as new name.
document.save_as(new_file_name, overwrite=True)
# to export to another support file_format (license permitting).
new_export_file_name = r"c:\temp\new_export_part"
document.export_data(new_export_file_name, "stp", overwrite=True)
# close document
document.close()
Using the context manager.
#! /usr/bin/python3.6
"""
Example 7:
Use the context manager to open CATIA documents and close
"""
import time
from pycatia import CATIADocHandler
catia_part = r'tests\cat_files\part_measurable.CATPart'
with CATIADocHandler(catia_part) as handler:
document = handler.document
# do some stuff.
# save if you need to.
time.sleep(5) # don't do this, no need.
# document is automatically closed. Lovely.
Find all .CATParts in folder and export to IGS.
#! /usr/bin/python3.6
"""
Example 8:
Open all CATParts in source directory and save to IGS in target directory.
"""
import os
from pycatia import CATIADocHandler
# make these directories the full pathname.
source_directory = 'tests/cat_files'
target_directory = '__junk__'
# if full paths are supplied above you should not do this.
source_directory = os.path.join(os.getcwd(), source_directory)
target_directory = os.path.join(os.getcwd(), target_directory)
# This loop assumes there are NO sub-directories.
for root, dirs, files in os.walk(source_directory):
for file in files:
# only convert CATParts.
if os.path.splitext(file)[1] == '.CATPart':
# create filename with path.
file_name = os.path.join(source_directory, file)
with CATIADocHandler(file_name) as handler:
document = handler.document
# create the full name of the target file, minus extension.
target_file = os.path.join(target_directory, os.path.splitext(file)[0])
# create the igs file in the __junk__ directory.
document.export_data(target_file, 'igs')
Get the position matrix of products (CATPart or CATProduct) in product.
#! /usr/bin/python3.6
"""
Example 9:
Get the position matrix of products (CATPart or CATProduct) in product.
"""
from pycatia import catia
documents = catia.documents
documents.open(r'tests\cat_files\product_top.CATProduct')
document = catia.active_document
product = document.product()
products = product.get_products()
for product in products:
print(product.position.get_components())
# print(product.name, position.get_components())
document.close()
Loop through a CATProduct and analyse children if CATPart.
#! /usr/bin/python3.6
"""
Example 10:
Loop through a CATProduct and analyse children if CATPart.
Only goes two levels deep.
"""
from pycatia import catia
from pycatia.enumeration.enumeration_types import cat_work_mode_type
documents = catia.documents
documents.open(r'tests\cat_files\product_top.CATProduct')
document = catia.active_document
product = document.product()
# Change the work mode to Design Mode.
# This is useful for CATIA configurations that work with a cache otherwise methods on children may fail
# due to the document not being loaded.
product.apply_work_mode(cat_work_mode_type.index("DESIGN_MODE"))
def print_properties(obj):
print(f"{obj.name}: mass: {obj.analyze.mass}, \n"
f" volume: {obj.analyze.volume}, \n"
f" wet_area: {obj.analyze.wet_area}, \n"
f" gravity_center: {obj.analyze.get_gravity_center()}, \n"
f" inertia: {obj.analyze.get_inertia()}"
)
# I know, this isn't pretty, but my intent is to keep examples simple.
for sub_product in product.get_products():
if sub_product.is_catproduct():
for child_product in sub_product.get_products():
if child_product.is_catpart():
child_product.activate_default_shape()
print_properties(child_product)
else:
sub_product.activate_default_shape()
print_properties(sub_product)
Move first child in product.
#! /usr/bin/python3.6
"""
Example 11:
Move the first child in product.
"""
from pycatia import catia
from pycatia.enumeration.enumeration_types import cat_work_mode_type
from pycatia.product_structure_interfaces.product import Product
documents = catia.documents
documents.open(r'tests\cat_files\product_top.CATProduct')
document = catia.active_document
product = document.product()
# Change the work mode to Design Mode.
# This is useful for CATIA configurations that work with a cache otherwise methods on children may fail
# due to the document not being loaded.
product.apply_work_mode(cat_work_mode_type.index("DESIGN_MODE"))
# Transformation matrix (45 degrees-rotation around the x axis and a translation).
transformation = (
1.000,
0,
0,
0,
0.707,
0.707,
0,
-0.707,
0.707,
10.000,
20.000,
30.000
)
# activates default shape on all children.
Product.activate_terminal_node(product.get_products())
# move the first child in parent.
product = product.get_products()[0]
move = product.move.apply(transformation)
Move first child in product.
#! /usr/bin/python3.7
"""
Example 12:
Access the CATIA COM object with a .CATPart open and and display
each parameter along with its name, value and its associated parameter set.
# todo: need to create a source part to support this example.
"""
from pycatia import catia
# from pycatia.knowledge_interfaces import BoolParam
documents = catia.documents
documents.open(r'tests/cat_files/part_measurable.CATPart')
document = catia.active_document
part = document.part()
# gets part parameters
part_parameters = part.parameters
# create parameter to activate pocket
part_parameters.create_boolean("Activate_Pocket", True)
# find and assign parameters
pocket_activity = part_parameters.item("Activate_Pocket")
# gets RootParameterSet inside of parameters
root_parameter_set = part_parameters.root_parameter_set
# ParameterSet(name="Parameters")
# gets ParameterSets inside of RootParameterSet
parameter_sets = root_parameter_set.parameter_sets
# gets all parameter sets inside of parameter_sets
sub_parameter_set = parameter_sets.items()
# [ParameterSet(name="Parameters_Pad"), ParameterSet(name="Parameters_Pocket")]
for parm_set in sub_parameter_set:
print(parm_set.name)
sub_parms = parm_set.all_parameters
for sub_item in sub_parms:
print(sub_item)
print("Parameter name: {} - Value: {}".format(sub_item.name, sub_item.value))
print()
# ParameterSet name: Parameters_Pad
# Parameter name: CF_Part_3\Parameters_Pad\Width| value: 120.0
# Parameter name: CF_Part_3\Parameters_Pad\Length| value: 60.0
# Parameter name: CF_Part_3\Parameters_Pad\Height| value: 10.0
# ParameterSet name: Parameters_Pocket
# Parameter name: CF_Part_3\Parameters_Pocket\Width| value: 30.0
# Parameter name: CF_Part_3\Parameters_Pocket\Length| value: 15.0
# Parameter name: CF_Part_3\Parameters_Pocket\Height| value: 10.0
# -----------------------------------------------------------------
# gets part relations
part_relations = part.relations
# create the parameter to which you want to assign a formula
sketch_pocket_activity = part_parameters.item("Sketches\\Sketch_Pocket\\Activity")
object_pocket_activity = part_parameters.item("PartBody\\Pocket.1\\Activity")
# create the formula to combine the sketch and pocket activity with the parameter <pocket_activity>
part_relations.create_formula("Activity_Sketch_Pocket",
"Checks weather the Pocket should be activated or not", sketch_pocket_activity,
pocket_activity.name)
part_relations.create_formula("Activity_Object_Pocket",
"Checks weather the Pocket should be activated or not", object_pocket_activity,
pocket_activity.name)
part.update()
# remove created formula
# -----------------------------------------------------------------
# part_relations.remove("Activity_Sketch_Pocket")
# part_relations.remove("Activity_Object_Pocket")
Move first child in product.
#! /usr/bin/python3.6
"""
Example 13:
3D Points, Spline, Extrusion and Generate Thickness.
"""
from pycatia import catia
# create a new part and rename.
documents = catia.documents
documents.add('Part')
document = catia.active_document
part = catia.active_document.part()
product = catia.active_document.product()
hybrid_shape_factory = part.hybrid_shape_factory
part_shape_factory = part.shape_factory
body = part.main_body
# create the hybrid bodies (Geometrical Set) to add our construction.
hybrid_bodies = part.hybrid_bodies
hybrid_body_points = hybrid_bodies.add()
hybrid_body_splines = hybrid_bodies.add()
hybrid_body_surface = hybrid_bodies.add()
hybrid_body_points.name = "construction_points"
hybrid_body_splines.name = "construction_splines"
hybrid_body_surface.name = "construction_surfaces"
# create the hybrid shape 'points'
point_1 = hybrid_shape_factory.add_new_point_coord(0, 0, 0)
point_2 = hybrid_shape_factory.add_new_point_coord(10, 5, 0)
point_3 = hybrid_shape_factory.add_new_point_coord(20, 0, 0)
point_4 = hybrid_shape_factory.add_new_point_coord(30, 5, 0)
point_5 = hybrid_shape_factory.add_new_point_coord(40, 0, 0)
# add the points to 'construction_points'
hybrid_body_points.append_hybrid_shape(point_1)
hybrid_body_points.append_hybrid_shape(point_2)
hybrid_body_points.append_hybrid_shape(point_3)
hybrid_body_points.append_hybrid_shape(point_4)
hybrid_body_points.append_hybrid_shape(point_5)
# create the spline
spline = hybrid_shape_factory.add_new_spline()
spline.add_point(point_1)
spline.add_point(point_2)
spline.add_point(point_3)
spline.add_point(point_4)
spline.add_point(point_5)
hybrid_body_splines.append_hybrid_shape(spline)
# create the extrusion
# plane used to define direction
plane = part.origin_elements.plane_xy
# have to create a direction object for extrusion.
direction = hybrid_shape_factory.add_new_direction(plane)
extrusion = hybrid_shape_factory.add_new_extrude(spline, i_offset_debut=10, i_offset_fin=10, i_direction=direction)
hybrid_body_surface.append_hybrid_shape(extrusion)
main_body = part.main_body
part.in_work_object = main_body
part_shape_factory.add_new_thick_surface(extrusion, 1, 5, 0)
part.update()
Using logging.
#! /usr/bin/python3.6
"""
Example 14:
Logging.
"""
from pycatia import catia
catia.logger.info('Hello world!')
catia.logger.warning('Stay alert, stay safe, bee kind!')
# [2020-06-13 11:12:09,096] INFO in example_14: Hello world!
# [2020-06-13 11:12:09,096] WARNING in example_14: Stay alert, stay safe, bee kind!
Draw a line between two points.
#! /usr/bin/python3.6
"""
Example 15:
Draw a line between two points.
"""
from pycatia import catia
documents = catia.documents
# create a new CATPart.
documents.add('Part')
document = catia.active_document
part = document.part()
hybrid_bodies = part.hybrid_bodies
hsf = part.hybrid_shape_factory
# create a new hybrid body.
geom_set = hybrid_bodies.add()
geom_set.name = 'Construction_Geometry'
co_ord_1 = (0, 0, 0)
co_ord_2 = (100, 0, 0)
point_1 = hsf.add_new_point_coord(co_ord_1[0], co_ord_1[1], co_ord_1[2])
point_2 = hsf.add_new_point_coord(co_ord_2[0], co_ord_2[1], co_ord_2[2])
geom_set.append_hybrid_shape(point_1)
geom_set.append_hybrid_shape(point_2)
line = hsf.add_new_line_pt_pt(point_1, point_2)
geom_set.append_hybrid_shape(line)
part.update()
Creates a square in a sketch and fully constrains it. Sketch then used to pad.
#! /usr/bin/python3.6
"""
Example 16:
Creates a square in a sketch and fully constrains it. Sketch then used to pad.
"""
from pycatia import catia
from pycatia.enumeration.enumeration_types import cat_constraint_type, cat_constraint_mode, cat_constraint_angle_sector
document = catia.active_document
part = document.part()
hsf = part.hybrid_shape_factory
hbs = part.hybrid_bodies
geom_set = hbs.add()
geom_set.name = "Construction Geometry"
# create a point at 0, 0, 0. This will be used to position square inside sketch.
co_ord = (0, 0, 0)
point_1_3D = hsf.add_new_point_coord(co_ord[0], co_ord[1], co_ord[2])
geom_set.append_hybrid_shape(point_1_3D)
# update the part otherwise point can't be referenced in sketch.
part.update()
# plane for sketch positioning
xy_plane = part.origin_elements.plane_xy
# add the sketch.
sketch = geom_set.hybrid_sketches.add(xy_plane)
# open the sketch for editing.
factory_2D = sketch.open_edition()
# get the h_direction used for constraining the line.
geom_elements = sketch.geometric_elements
abs_axis = geom_elements.item("AbsoluteAxis")
h_direction = abs_axis.get_item("HDirection")
v_direction = abs_axis.get_item("VDirection")
h_direction.report_name = 1
v_direction.report_name = 2
# create the points for the line.
point_1 = factory_2D.create_point(0, 0)
point_2 = factory_2D.create_point(0, 10)
point_3 = factory_2D.create_point(10, 10)
point_4 = factory_2D.create_point(10, 0)
point_1.report_name = 3
point_2.report_name = 4
point_3.report_name = 5
point_4.report_name = 6
# as the lines start and end points will be set to the points above.
line_1 = factory_2D.create_line(0, 0, 0, 10)
line_2 = factory_2D.create_line(0, 10, 10, 10)
line_3 = factory_2D.create_line(10, 10, 10, 0)
line_4 = factory_2D.create_line(10, 0, 0, 0)
line_1.report_name = 7
line_2.report_name = 8
line_3.report_name = 9
line_4.report_name = 10
line_1.start_point, line_1.end_point = point_1, point_2
line_2.start_point, line_2.end_point = point_2, point_3
line_3.start_point, line_3.end_point = point_3, point_4
line_4.start_point, line_4.end_point = point_4, point_1
# lets start constraining the square.
constraints = sketch.constraints
# create the length constraint.
# left vertical line.
constraint_length_1 = constraints.add_mono_elt_cst(cat_constraint_type.index("catCstTypeLength"), line_1)
# horizontal line.
constraint_length_4 = constraints.add_mono_elt_cst(cat_constraint_type.index("catCstTypeLength"), line_4)
# make the constraint reference.
# constraint_length_1.mode = cat_constraint_mode.index("catCstModeDrivenDimension")
# # this is how you would change the length of the line via it's constraint.
# length = constraint_length_1.dimension
# length.value = 20
# constrain the bottom line to h_direction
constraint_horizontal = constraints.add_bi_elt_cst(
cat_constraint_type.index("catCstTypeOn"),
line_4,
h_direction
)
# constrain left vertical as angle to bottom line.
constraint_angle = constraints.add_bi_elt_cst(cat_constraint_type.index("catCstTypeAngle"), line_1, line_4)
# make the two horizontal lines parallel.
constraint_p_h = constraints.add_bi_elt_cst(cat_constraint_type.index("catCstTypeParallelism"), line_1, line_3)
# make the two vertical lines parallel.
constraint_p_v = constraints.add_bi_elt_cst(cat_constraint_type.index("catCstTypeParallelism"), line_2, line_4)
# create projection of 3D point used and constrain to 2d point.
geometric_elements1 = factory_2D.create_projections(point_1_3D)
projected_point = geometric_elements1.item(1)
projected_point.construction = True
point_ref = part.create_reference_from_object(projected_point)
constraint_mid = constraints.add_bi_elt_cst(
cat_constraint_type.index("catCstTypeOn"),
point_1,
point_ref
)
sketch.close_edition()
part.update()
# create the pad from sketch
main_body = part.main_body
part.in_work_object = main_body
part_shape_factory = part.shape_factory
part_shape_factory.add_new_pad(sketch, 5)
part.update()
Creates a drawing border for an A0 landscape page.
#! /usr/bin/python3.6
"""
Example 17:
Drafting: create a border template in the background view of the currently opened A0 landscape CATDrawing.
"""
from pycatia import catia
from pycatia.exception_handling import CATIAApplicationException
from pycatia.enumeration.enumeration_types import cat_text_anchor_position
from pycatia.enumeration.enumeration_types import cat_paper_orientation
from pycatia.enumeration.enumeration_types import cat_paper_size
a0_x = 1189
a0_y = 841
document = catia.active_document
drawing = document.drawing_root()
sheets = drawing.sheets
sheet = sheets.active_sheet
if cat_paper_orientation[sheet.orientation] != 'catPaperLandscape':
raise CATIAApplicationException('Sheet orientation is not landscape.')
if cat_paper_size[sheet.paper_size] != 'catPaperA0':
raise CATIAApplicationException('Sheet size is not A0.')
views = sheet.views
main_view = views.get_item_by_name('Main View')
background_view = views.get_item_by_name('Background View')
background_view.activate()
factory_2d = background_view.factory_2d
# create the outside border at page limits.
# bottom
ob_line_1 = factory_2d.create_line(0, 0, a0_x, 0)
ob_line_2 = factory_2d.create_line(a0_x, 0, a0_x, a0_y)
ob_line_3 = factory_2d.create_line(a0_x, a0_y, 0, a0_y)
ob_line_4 = factory_2d.create_line(0, a0_y, 0, 0)
# create inner border offset by 10mm
offset = 10
ib_line_1 = factory_2d.create_line(offset, offset, a0_x - offset, offset)
ib_line_2 = factory_2d.create_line(a0_x - offset, offset, a0_x - offset, a0_y - offset)
ib_line_3 = factory_2d.create_line(a0_x - offset, a0_y - offset, offset, a0_y - offset)
ib_line_4 = factory_2d.create_line(offset, a0_y - offset, offset, offset)
# add some lines for grid referencing
h_splits = 5
v_splits = 4
h_offset = a0_x / h_splits
v_offset = a0_y / v_splits
h_split_1 = factory_2d.create_line(h_offset, 0, h_offset, offset)
h_split_2 = factory_2d.create_line(h_offset * 2, 0, h_offset * 2, offset)
h_split_3 = factory_2d.create_line(h_offset * 3, 0, h_offset * 3, offset)
h_split_4 = factory_2d.create_line(h_offset * 4, 0, h_offset * 4, offset)
h_split_5 = factory_2d.create_line(h_offset, a0_y - offset, h_offset, a0_y)
h_split_6 = factory_2d.create_line(h_offset * 2, a0_y - offset, h_offset * 2, a0_y)
h_split_7 = factory_2d.create_line(h_offset * 3, a0_y - offset, h_offset * 3, a0_y)
h_split_8 = factory_2d.create_line(h_offset * 4, a0_y - offset, h_offset * 4, a0_y)
v_split_1 = factory_2d.create_line(0, v_offset, offset, v_offset)
v_split_2 = factory_2d.create_line(0, v_offset * 2, offset, v_offset * 2)
v_split_3 = factory_2d.create_line(0, v_offset * 3, offset, v_offset * 3)
v_split_4 = factory_2d.create_line(a0_x, v_offset, a0_x - offset, v_offset)
v_split_5 = factory_2d.create_line(a0_x, v_offset * 2, a0_x - offset, v_offset * 2)
v_split_6 = factory_2d.create_line(a0_x, v_offset * 2, a0_x - offset, v_offset * 2)
# add some text to the border
anchor_position = cat_text_anchor_position.index('catMiddleCenter')
texts = background_view.texts
# create a function to change the font by using the DrawingTextProperties class of DrawingText.
def change_text_properties(text):
"""
:param text: DrawingText
:return:
"""
text_properties = text.text_properties
text_properties.font_name = 'SSS1'
text_properties.font_size = 3.5
return text
text_e_l = texts.add('E', h_offset / 2, offset / 2)
text_e_l.anchor_position = anchor_position
text_e_l = change_text_properties(text_e_l)
text_d_l = texts.add('D', h_offset / 2 + h_offset, offset / 2)
text_d_l.anchor_position = anchor_position
text_d_l = change_text_properties(text_d_l)
text_c_l = texts.add('C', h_offset / 2 + (h_offset * 2), offset / 2)
text_c_l.anchor_position = anchor_position
text_c_l = change_text_properties(text_c_l)
text_b_l = texts.add('B', h_offset / 2 + (h_offset * 3), offset / 2)
text_b_l.anchor_position = anchor_position
text_b_l = change_text_properties(text_b_l)
text_a_l = texts.add('A', h_offset / 2 + (h_offset * 4), offset / 2)
text_a_l.anchor_position = anchor_position
text_a_l = change_text_properties(text_a_l)
text_e_u = texts.add('E', h_offset / 2, a0_y - (offset / 2))
text_e_u.anchor_position = anchor_position
text_e_u = change_text_properties(text_e_u)
text_d_u = texts.add('D', h_offset / 2 + h_offset, a0_y - (offset / 2))
text_d_u.anchor_position = anchor_position
text_d_u = change_text_properties(text_d_u)
text_c_u = texts.add('C', h_offset / 2 + (h_offset * 2), a0_y - (offset / 2))
text_c_u.anchor_position = anchor_position
text_c_u = change_text_properties(text_c_u)
text_b_u = texts.add('B', h_offset / 2 + (h_offset * 3), a0_y - (offset / 2))
text_b_u.anchor_position = anchor_position
text_b_u = change_text_properties(text_b_u)
text_a_u = texts.add('A', h_offset / 2 + (h_offset * 4), a0_y - (offset / 2))
text_a_u.anchor_position = anchor_position
text_a_u = change_text_properties(text_a_u)
text_1_l = texts.add('1', offset / 2, v_offset / 2)
text_1_l.anchor_position = anchor_position
text_1_l = change_text_properties(text_1_l)
text_2_l = texts.add('2', offset / 2, v_offset / 2 + v_offset)
text_2_l.anchor_position = anchor_position
text_2_l = change_text_properties(text_2_l)
text_3_l = texts.add('3', offset / 2, v_offset / 2 + (v_offset * 2))
text_3_l.anchor_position = anchor_position
text_3_l = change_text_properties(text_3_l)
text_4_l = texts.add('3', offset / 2, v_offset / 2 + (v_offset * 3))
text_4_l.anchor_position = anchor_position
text_4_l = change_text_properties(text_4_l)
text_1_r = texts.add('1', a0_x - (offset / 2), v_offset / 2)
text_1_r.anchor_position = anchor_position
text_1_r = change_text_properties(text_1_r)
text_2_r = texts.add('2', a0_x - (offset / 2), v_offset / 2 + v_offset)
text_2_r.anchor_position = anchor_position
text_2_r = change_text_properties(text_2_r)
text_3_r = texts.add('3', a0_x - (offset / 2), v_offset / 2 + (v_offset * 2))
text_3_r.anchor_position = anchor_position
text_3_r = change_text_properties(text_3_r)
text_4_r = texts.add('3', a0_x - (offset / 2), v_offset / 2 + (v_offset * 3))
text_4_r.anchor_position = anchor_position
text_4_r = change_text_properties(text_4_r)
main_view.activate()
sheet.force_update()
Reorder a Product tree alphabetically
#! /usr/bin/python3.6
"""
Example 17:
Drafting: create a border template in the background view of the currently opened A0 landscape CATDrawing.
"""
from pycatia import catia
from pycatia.exception_handling import CATIAApplicationException
from pycatia.enumeration.enumeration_types import cat_text_anchor_position
from pycatia.enumeration.enumeration_types import cat_paper_orientation
from pycatia.enumeration.enumeration_types import cat_paper_size
a0_x = 1189
a0_y = 841
document = catia.active_document
drawing = document.drawing_root()
sheets = drawing.sheets
sheet = sheets.active_sheet
if cat_paper_orientation[sheet.orientation] != 'catPaperLandscape':
raise CATIAApplicationException('Sheet orientation is not landscape.')
if cat_paper_size[sheet.paper_size] != 'catPaperA0':
raise CATIAApplicationException('Sheet size is not A0.')
views = sheet.views
main_view = views.get_item_by_name('Main View')
background_view = views.get_item_by_name('Background View')
background_view.activate()
factory_2d = background_view.factory_2d
# create the outside border at page limits.
# bottom
ob_line_1 = factory_2d.create_line(0, 0, a0_x, 0)
ob_line_2 = factory_2d.create_line(a0_x, 0, a0_x, a0_y)
ob_line_3 = factory_2d.create_line(a0_x, a0_y, 0, a0_y)
ob_line_4 = factory_2d.create_line(0, a0_y, 0, 0)
# create inner border offset by 10mm
offset = 10
ib_line_1 = factory_2d.create_line(offset, offset, a0_x - offset, offset)
ib_line_2 = factory_2d.create_line(a0_x - offset, offset, a0_x - offset, a0_y - offset)
ib_line_3 = factory_2d.create_line(a0_x - offset, a0_y - offset, offset, a0_y - offset)
ib_line_4 = factory_2d.create_line(offset, a0_y - offset, offset, offset)
# add some lines for grid referencing
h_splits = 5
v_splits = 4
h_offset = a0_x / h_splits
v_offset = a0_y / v_splits
h_split_1 = factory_2d.create_line(h_offset, 0, h_offset, offset)
h_split_2 = factory_2d.create_line(h_offset * 2, 0, h_offset * 2, offset)
h_split_3 = factory_2d.create_line(h_offset * 3, 0, h_offset * 3, offset)
h_split_4 = factory_2d.create_line(h_offset * 4, 0, h_offset * 4, offset)
h_split_5 = factory_2d.create_line(h_offset, a0_y - offset, h_offset, a0_y)
h_split_6 = factory_2d.create_line(h_offset * 2, a0_y - offset, h_offset * 2, a0_y)
h_split_7 = factory_2d.create_line(h_offset * 3, a0_y - offset, h_offset * 3, a0_y)
h_split_8 = factory_2d.create_line(h_offset * 4, a0_y - offset, h_offset * 4, a0_y)
v_split_1 = factory_2d.create_line(0, v_offset, offset, v_offset)
v_split_2 = factory_2d.create_line(0, v_offset * 2, offset, v_offset * 2)
v_split_3 = factory_2d.create_line(0, v_offset * 3, offset, v_offset * 3)
v_split_4 = factory_2d.create_line(a0_x, v_offset, a0_x - offset, v_offset)
v_split_5 = factory_2d.create_line(a0_x, v_offset * 2, a0_x - offset, v_offset * 2)
v_split_6 = factory_2d.create_line(a0_x, v_offset * 2, a0_x - offset, v_offset * 2)
# add some text to the border
anchor_position = cat_text_anchor_position.index('catMiddleCenter')
texts = background_view.texts
# create a function to change the font by using the DrawingTextProperties class of DrawingText.
def change_text_properties(text):
"""
:param text: DrawingText
:return:
"""
text_properties = text.text_properties
text_properties.font_name = 'SSS1'
text_properties.font_size = 3.5
return text
text_e_l = texts.add('E', h_offset / 2, offset / 2)
text_e_l.anchor_position = anchor_position
text_e_l = change_text_properties(text_e_l)
text_d_l = texts.add('D', h_offset / 2 + h_offset, offset / 2)
text_d_l.anchor_position = anchor_position
text_d_l = change_text_properties(text_d_l)
text_c_l = texts.add('C', h_offset / 2 + (h_offset * 2), offset / 2)
text_c_l.anchor_position = anchor_position
text_c_l = change_text_properties(text_c_l)
text_b_l = texts.add('B', h_offset / 2 + (h_offset * 3), offset / 2)
text_b_l.anchor_position = anchor_position
text_b_l = change_text_properties(text_b_l)
text_a_l = texts.add('A', h_offset / 2 + (h_offset * 4), offset / 2)
text_a_l.anchor_position = anchor_position
text_a_l = change_text_properties(text_a_l)
text_e_u = texts.add('E', h_offset / 2, a0_y - (offset / 2))
text_e_u.anchor_position = anchor_position
text_e_u = change_text_properties(text_e_u)
text_d_u = texts.add('D', h_offset / 2 + h_offset, a0_y - (offset / 2))
text_d_u.anchor_position = anchor_position
text_d_u = change_text_properties(text_d_u)
text_c_u = texts.add('C', h_offset / 2 + (h_offset * 2), a0_y - (offset / 2))
text_c_u.anchor_position = anchor_position
text_c_u = change_text_properties(text_c_u)
text_b_u = texts.add('B', h_offset / 2 + (h_offset * 3), a0_y - (offset / 2))
text_b_u.anchor_position = anchor_position
text_b_u = change_text_properties(text_b_u)
text_a_u = texts.add('A', h_offset / 2 + (h_offset * 4), a0_y - (offset / 2))
text_a_u.anchor_position = anchor_position
text_a_u = change_text_properties(text_a_u)
text_1_l = texts.add('1', offset / 2, v_offset / 2)
text_1_l.anchor_position = anchor_position
text_1_l = change_text_properties(text_1_l)
text_2_l = texts.add('2', offset / 2, v_offset / 2 + v_offset)
text_2_l.anchor_position = anchor_position
text_2_l = change_text_properties(text_2_l)
text_3_l = texts.add('3', offset / 2, v_offset / 2 + (v_offset * 2))
text_3_l.anchor_position = anchor_position
text_3_l = change_text_properties(text_3_l)
text_4_l = texts.add('3', offset / 2, v_offset / 2 + (v_offset * 3))
text_4_l.anchor_position = anchor_position
text_4_l = change_text_properties(text_4_l)
text_1_r = texts.add('1', a0_x - (offset / 2), v_offset / 2)
text_1_r.anchor_position = anchor_position
text_1_r = change_text_properties(text_1_r)
text_2_r = texts.add('2', a0_x - (offset / 2), v_offset / 2 + v_offset)
text_2_r.anchor_position = anchor_position
text_2_r = change_text_properties(text_2_r)
text_3_r = texts.add('3', a0_x - (offset / 2), v_offset / 2 + (v_offset * 2))
text_3_r.anchor_position = anchor_position
text_3_r = change_text_properties(text_3_r)
text_4_r = texts.add('3', a0_x - (offset / 2), v_offset / 2 + (v_offset * 3))
text_4_r.anchor_position = anchor_position
text_4_r = change_text_properties(text_4_r)
main_view.activate()
sheet.force_update()