Hello,
I was wondering if there is any way for me to open or read CAD files locally into CATIA. Currently, I have the following script(most relevant part at the bottom highlighted), where I have success interacting with CATIA(3D EXPERIENCE CATIA Part Design Plus) by running my script in Visual Studio Code. I have tried a few file formats (.step, CATPart, .3dxml).
# Just need Geometry access to CATIA from Python
import win32com.client
# Import os to interact with operating system
import os
# Connect ot CATIA/3DEXPERIENCE
CATIA = win32com.client.Dispatch("CATIA.Application")
print("Connected to CATIA")
# Get Active editor
myEditor = CATIA.ActiveEditor
print("Active Editor:", myEditor)
# Get Product Service
myProdService = myEditor.GetService("PLMProductService")
print("Product Service:", myProdService)
# Get Edited Content
myEntities = myProdService.EditedContent
print("Edited content:",myEntities)
print("Number of Entities:", myEntities.Count)
# Retrieve and then Inspect Entity
entity = myEntities.Item(1)
print("Entity:", entity)
try:
print("Name:",entity.Name)
except:
print("No Name attribute")
try:
print("Type:",entity.Type)
except:
print("No Type attribute")
#print(dir(entity)) # Prints all attributes
print("3DX object Type:",entity.GetCustomType())
reps = entity.RepInstances
print("RepInstances count:",reps.Count)
for i in range(1, reps.Count + 1):
rep = reps.Item(i)
print(i,':', rep.Name)
try:
print("custom type:", rep.GetCustomType())
except:
print("No Custom Type")
#print(dir(rep)) # Prints all attributes of rep
# Get reference of instance
repRef = rep.ReferenceinstanceOf
print("repRef:",repRef)
try:
print("repRef name:", repRef.Name)
except:
print("repRef has no name")
try:
print("repRef custom type:", repRef.GetCustomType())
except:
print("No repRef custom type")
#print(dir(repRef)) # Prints all attributes of repRef
obj = repRef.GetItem("Part")
print("Part","Found:",obj)
# We have succesfully reached the actual part object
# print(dir(obj)) # Prints all attributes of actual part object
bodies = obj.Bodies
print("Bodies count:", bodies.Count)
for i in range(1, bodies.Count + 1):
body = bodies.Item(i)
print(i,":",body.Name)
try:
shapes = body.Shapes
print("Shapes Count:",shapes.Count)
except Exception as e:
print("Could not access shapes:",e)
main_body = obj.MainBody
print("Main body:", main_body.Name)
# Even though you get PartBody from .MainBody, Body.2 is the actual geometry
workpiece = bodies.Item("Body.2")
# Create body reference
body_ref = obj.CreateReferenceFromObject(workpiece)
#print(dir(workpiece))
shapes = workpiece.Shapes
#print(dir(shapes))
print("Shapes count:",shapes.Count)
for k in range(1, shapes.Count + 1):
shape = shapes.Item(k)
shape_ref = obj.CreateReferenceFromObject(shape)
print(k,":",shape.Name)
print(dir(shape))
print(body_ref)
print(shape_ref)
# Import part file into CATIA
CATIA.Visible = True
cad_file = r"file path"
# Test to see if the file path exists
print("File exists: ", os.path.exists(cad_file))
try:
doc = CATIA.Documents.Open(cad_file)
print("Opened with Documents.Open")
except Exception as e:
print("Open failed:",e)
try:
doc = CATIA.Documents.Read(cad_file)
except Exception as e2:
print("Read also failed:",e2)
raise
print("Document name:",doc.Name)
# Heirarchy for accessing 3DShape object
# VPMReference -> VPMRepInstance -> 3DShape
#ENUM = win32com.client.constants
