0

Trying to get the coordinates of a specific point inside Geometry of the Sketch1 of a PartBody in Catia V5 Part.

import win32com.client
from pycatia import catia


# Open the CATIA document
caa = catia()


# Open the CATIA document
documents = win32com.client.Dispatch('CATIA.Application').Documents
file_name = './Part.CATPart'

part_document = documents.Open(file_name)
part = part_document.Part
bodies = part.Bodies
body = bodies.Item('PartBody')  

# Access the Sketch
sketches = body.Sketches
sketch = sketches.Item('Sketch.1') 

# Open the sketch for edition
factory2D = sketch.OpenEdition()

# Access the geometric elements in the sketch
geometric_elements = sketch.GeometricElements

# Find the specific point by name
point_name = 'Point.22'  
point = None

for i in range(1, geometric_elements.Count + 1):
    element = geometric_elements.Item(i)
    if element.Name == point_name:
        point = element
        break

# Check if the point was found and retrieve its coordinates
if point is not None:
    try:
        # Assuming the point has X, Y properties directly accessible
        x = point.X
        y = point.Y
        print(f"Coordinates of {point_name}: X={x}, Y={y}")
    except AttributeError as e:
        print(f"{point_name} found but coordinates could not be retrieved: {e}")
    try:
        coord = point.GetCoordinates()
    except Exception as e:
        print(e)
else:
    print(f"{point_name} not found in the sketch.")

# Close the sketch edition
sketch.CloseEdition()

This raises errors, first one I don't know how to access point coordinates so I am trying point.X, but this raises: Point.122 found but coordinates could not be retrieved: Item.X . Also trying the method GetCoordinates() but this raises (-2147352567, 'Ocurrió una excepción.', (0, 'CATIAPoint2D', 'The method GetCoordinates failed', None, 0, -2147467259), None) .

Any sugestion?

2
  • I’m not used to python, but in VBA for GetCoordinates you need to hand over an array to this routine. It is not a function which returns values. So maybe it is the same in python. Commented Aug 1, 2024 at 10:54
  • Does your sketch.GeometricElements only contain points ? I suggest you to use sélection.search command with a query inside of your sketch, then you can get coordonates by mesurable :looping on each selected items. (Or make sure 'element' is a point in your sample) stackoverflow.com/questions/77086257/… Commented Aug 1, 2024 at 18:10

1 Answer 1

0

There are issues with your code. Most notably you keep switching between using pycatia's in built methods and using the win32.client methods. When using pycatia you do not need to do this. I recommend you work your way through the examples in the pycatia repository to get a better idea of it's usage.

For example:

sketch.GeometricElements
element.Name

Note the use of the uppercase GeometricElements and Name? pycaita does not use uppercase Class methods like this and follows pythons naming conventions.

Now, onto the problem itself. When you get the point from the GeometricElements collection you're simply getting a GeometricElement which just has one property geometric_type. You then need to create a Point2D object from the element if it is indeed a point. You'll then have available to you the attributes of a Point2D object.

The following should work. This code requires an open CATPart with a sketch named Sketch.1. Within the sketch there needs to be a point named Point.1.


from pycatia import catia
from pycatia.enumeration.enumeration_types import cat_geometric_type
from pycatia.mec_mod_interfaces.part_document import PartDocument
from pycatia.sketcher_interfaces.point_2D import Point2D

# Open the CATIA document
caa = catia()
part_document: PartDocument = caa.active_document
part = part_document.part
bodies = part.bodies
body = bodies.item('PartBody')

# Access the Sketch
sketches = body.sketches
sketch = sketches.item('Sketch.1')

# Access the geometric elements in the sketch
geometric_elements = sketch.geometric_elements

# Find the specific point by name
point_name = 'Point.1'

element = geometric_elements.item(point_name)

if cat_geometric_type[element.geometric_type] == 'catGeoTypePoint2D':
    point_2d = Point2D(element.com_object)
    print(point_2d.get_coordinates())
else:
    print('Element is not a point.')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.