evereux / pycatia

python module for CATIA V5 automation
MIT License
204 stars 53 forks source link

[BUG] Drawing Dimensions.Add Error #229

Closed siddhu2310 closed 3 months ago

siddhu2310 commented 3 months ago

Describe the bug I have created two points in the Drawing, then tried to generate the Dimensions for it, Just I need to show the Distance Between the Points.

Error: ''' Traceback (most recent call last): File "d:\Tesing\Drwing\todrw.py", line 86, in my_dimension = drawing_view.dimensions.add(catDimDistance, elements, selpoints, catDimDistanceMin) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python311\Lib\site-packages\pycatia\drafting_interfaces\drawing_dimensions.py", line 113, in add self.drawing_dimensions.Add( File "<COMObject >", line 2, in Add TypeError: must be real number, not Point2D '''

Code

from pycatia import catia
from pycatia.drafting_interfaces.drawing_document import DrawingDocument,DrawingSheets
from pycatia.drafting_interfaces.drawing_root import DrawingRoot
from pycatia.drafting_interfaces.drawing_view import DrawingView,DrawingViewGenerativeBehavior,DrawingViewGenerativeLinks
from pycatia.system_interfaces.any_object import AnyObject
from pycatia.enumeration.enumeration_types import cat_paper_orientation, cat_paper_size,cat_drawing_standard,cat_dim_type
from pycatia.mec_mod_interfaces.axis_systems import AxisSystems
from pycatia.mec_mod_interfaces.part import Part

# Initialize CATIA application
caa = catia()

# Create a new drawing document
documents = caa.documents
drawing_document = documents.add("Drawing")
drawing_document= DrawingDocument(drawing_document.com_object)
# Set the drawing standard to ISO
drawing_document.standard = cat_drawing_standard.index("catISO")

# Set paper size and orientation
sheets = drawing_document.sheets
sheet = sheets.active_sheet
sheet.paper_size = cat_paper_size.index("catPaperA3")
sheet.orientation = cat_paper_orientation.index("catPaperLandscape")

# Create a new view
views = sheet.views
drawing_view = views.add("AutomaticNaming")

# Set view properties
drawing_view.x = 420 / 2
drawing_view.y = 297 / 2
drawing_view.scale_2 = 3
drawing_view.frame_visualization = False

# Access generative links and behavior
generative_links = drawing_view.generative_links
generative_behavior = drawing_view.generative_behavior

# Define the part document and set the axis system
part_document = documents.item("DEMO_1.CATPart")
part = Part(part_document.part.com_object)

axis_systems = AxisSystems(part.axis_systems.com_object)
axis_system = axis_systems.get_item("Axis Sys06")

ipro = AnyObject(None)
generative_behavior.set_axis_systeme(ipro,axis_system)

# Add links to the body
bodies = part.bodies
body1 = bodies.get_item("Part1")
body2 = bodies.get_item("Part2")

generative_links.add_link(body1)
generative_links.add_link(body2)

# Define front view and update
generative_behavior.define_front_view(-0.087122, 0.996198, 0, -0.002424, -0.000212, -0.999997)
generative_behavior.update()

drawing_view.activate()

# Define the points P1 and P2
P1X, P1Y = 15.00341, 10.40991
P2X, P2Y = 5.671, 14.00241

# Create points and line
factory_2d = drawing_view.factory_2d
origine = factory_2d.create_point(0, 0)
point_p1 = factory_2d.create_point(P1X, P1Y)
point_p2 = factory_2d.create_point(P2X, P2Y)
line_2d = factory_2d.create_line(P2X, P2Y, P1X, P1Y)

catDimDistance = cat_dim_type.index('catDimDistance')
catDimDistanceMin = cat_dim_type.index('catDimDistanceMin')

# Create a dimension between points
selection = drawing_document.selection
selection.clear()
elements = (origine, line_2d)
selpoints = [0, 0, 0, 0]
my_dimension = drawing_view.dimensions.add(catDimDistance, elements, selpoints, catDimDistanceMin)

# Force update
generative_behavior.force_update()

# Reframe the view
active_window = caa.active_window
active_viewer = active_window.active_viewer
active_viewer.reframe()

Desktop (please complete the following information):

siddhu2310 commented 3 months ago

Equivalent CATIA VBA Code

Sub CATMain()

    Dim Fact2D As Factory2D

    Dim documents1 As Documents
    Set documents1 = CATIA.Documents

    Dim drawingDocument1 As drawingDocument
    Set drawingDocument1 = documents1.Add("Drawing")

    drawingDocument1.Standard = catISO

    Dim drawingSheets1 As drawingSheets
    Set drawingSheets1 = drawingDocument1.Sheets

    Dim drawingSheet1 As drawingSheet
    Set drawingSheet1 = drawingSheets1.Item("Sheet.1")

    drawingSheet1.PaperSize = catPaperA3
    drawingSheet1.Orientation = catPaperLandscape

    Dim drawingViews1 As drawingViews
    Set drawingViews1 = drawingSheet1.Views

    'Sidd
    'Dim drawingView1 As drawingView
    Set drawingView1 = drawingViews1.Add("AutomaticNaming")

    Set Fact2D = drawingView1.Factory2D

    drawingView1.X = 420 / 2
    drawingView1.Y = 297 / 2

    drawingView1.FrameVisualization = False
    drawingView1.Scale2 = 3

    Dim drawingViewGenerativeLinks1 As DrawingViewGenerativeLinks
    Set drawingViewGenerativeLinks1 = drawingView1.GenerativeLinks

    Dim drawingViewGenerativeBehavior1 As DrawingViewGenerativeBehavior
    Set drawingViewGenerativeBehavior1 = drawingView1.GenerativeBehavior

    Dim partDocument1 As partDocument
    Set partDocument1 = documents1.Item("DEMO_1.CATPart")

    Dim part1 As part
    Set part1 = partDocument1.part

    Dim axisSystems1 As axisSystems
    Set axisSystems1 = part1.axisSystems

    Dim axisSystem1 As axisSystem

    Set axisSystem1 = axisSystems1.Item("Axis Sys06")

    drawingViewGenerativeBehavior1.SetAxisSysteme Nothing, axisSystem1

    Dim bodies1 As Bodies
    Set bodies1 = part1.Bodies

    Dim body1 As Body
    Set body1 = bodies1.Item("Part1")

    drawingViewGenerativeLinks1.AddLink body1

    Dim body2 As Body
    Set body2 = bodies1.Item("Part2")

    drawingViewGenerativeLinks1.AddLink body2

    drawingViewGenerativeBehavior1.DefineFrontView -0.087122, 0.996198, 0#, -0.002424, -0.000212, -0.999997
    drawingViewGenerativeBehavior1.Update

    drawingView1.Activate

    Dim P1X As Double, P1Y As Double
    Dim P2X As Double, P2Y As Double

    P1X = 15.00341
    P1Y = 10.40991
    P2X = 5.671
    P2Y = 14.00241

Dim Point1 As Point2D
Dim Point2 As Point2D
Dim Line2D As Line2D
Dim myElements
Dim selpoints
Dim MyDimension As DrawingDimension

 Set origine = Fact2D.CreatePoint(0, 0)
 Set PointP2 = Fact2D.CreatePoint(P2X, P2Y)
 Set PointP1 = Fact2D.CreatePoint(P1X, P1Y)

 Set Line2D = Fact2D.CreateLine(P2X, P2Y, P1X, P1Y)

 myElements = Array(origine, Line2D)
 selpoints = Array(0, 0, 0, 0)

 Set MyDimension = drawingView1.Dimensions.Add(catDimDistance, myElements, selpoints, catDimDistanceMin)

    drawingViewGenerativeBehavior1.ForceUpdate

    Dim specsAndGeomWindow1 As SpecsAndGeomWindow
    Set specsAndGeomWindow1 = CATIA.ActiveWindow

    Dim specsViewer1 As SpecsViewer
    Set specsViewer1 = specsAndGeomWindow1.ActiveViewer

    specsViewer1.Reframe

End Sub
siddhu2310 commented 3 months ago

I have Solved the Issue Myself, Just Passed Com _Object

elements = (origine.com_object,line_2d.com_object)
evereux commented 3 months ago

I'm re-opening this issue as you shouldn't have to do that as an end user. That should be done within the class method itself so I'll get this patched soon. However, the update will break your current solution.

evereux commented 3 months ago

There's been some ninja editing here ... The original question was regarding the DrawingDimensions.add2() class method. Both require fixing.

evereux commented 3 months ago

I've submitted fixes for both DrawingDimensions.add() and DrawingDimensions.add2() to the developement branch which will be included in the next 0.74 release.

Here are complete working examples for both. These require an already open CATDrawing with an active view.

DrawingDimensions.add()

from pycatia import catia
from pycatia.enumeration.enumeration_types import cat_dim_type
from pycatia.enumeration.enumeration_types import cat_dim_line_rep
from pycatia.drafting_interfaces.drawing_document import DrawingDocument

caa = catia()
drawing_document: DrawingDocument = caa.active_document
sheets = drawing_document.sheets
sheet = sheets.active_sheet
views = sheet.views
active_view = views.active_view
factory_2d = active_view.factory_2d

line_1 = factory_2d.create_line(50, 10, 150, 10)
line_2 = factory_2d.create_line(50, 10, 120, 100)
elipse = factory_2d.create_ellipse(-40, 100, 120, 180, 120, 90, 0, 3)
point_1 = factory_2d.create_point(-10, 190)
point_2 = factory_2d.create_point(-120, 190)

catDimAngle = cat_dim_type.index('catDimAngle')
catDimAuto = cat_dim_line_rep.index('catDimAuto')

line_elements = (line_1, line_2)
selection_points_1 = (150, 10, 120, 100)

active_view.dimensions.add(catDimAngle, line_elements, selection_points_1, catDimAuto)

catDimLengthCurvilinear = cat_dim_type.index('catDimLengthCurvilinear')
catDimOffset = cat_dim_line_rep.index('catDimOffset')

var_elements = (point_1, point_2, elipse)
selection_points_2 = (0, 0, 0, 0)
active_view.dimensions.add(catDimLengthCurvilinear, var_elements, selection_points_2, catDimOffset)

DrawingDimensions.add2()


from pycatia import catia
from pycatia.enumeration.enumeration_types import cat_dim_type
from pycatia.enumeration.enumeration_types import cat_dim_line_rep
from pycatia.drafting_interfaces.drawing_document import DrawingDocument

caa = catia()
drawing_document: DrawingDocument = caa.active_document
sheets = drawing_document.sheets
sheet = sheets.active_sheet
views = sheet.views
active_view = views.active_view
factory_2d = active_view.factory_2d

point_1 = factory_2d.create_point(40, 230)
point_2 = factory_2d.create_point(80, 210)
line_1 = factory_2d.create_line(50, 10, 150, 10)

catDimDistance = cat_dim_type.index('catDimDistance')

point_elements = (point_1, point_2)
selection_points = (0, 0, 0, 0)

dimension = active_view.dimensions.add2(catDimDistance, point_elements, selection_points, line_1, 0)
siddhu2310 commented 3 months ago

Thank you so much for the support @evereux When I can expect the 0.7.4 Release ?

evereux commented 3 months ago

This has now been released.