Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Inventor API Python

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
22351396
267 Views, 2 Replies

Inventor API Python

I tried to create an assembly joint. It connects the midpoints of the edges of two faces using a rotational joint. I used python. 
My problem is that "Error creating joint definition: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147467259), None)"
Here is my code.

import win32com.client

def AssemblyJoint():
    # Initialize Inventor application
    invApp = win32com.client.Dispatch("Inventor.Application")
    
    # Create a new assembly document
    asmDoc = invApp.Documents.Add(12291, invApp.FileManager.GetTemplateFile(12291, 8962))
    asmDef = asmDoc.ComponentDefinition
    tg = invApp.TransientGeometry
    
    # Place an occurrence into the assembly
    matrix1 = tg.CreateMatrix()
    translation_vector1 = tg.CreateVector(0, 0, 0)
    matrix1.SetTranslation(translation_vector1)
    occ1 = asmDef.Occurrences.Add("C:\\Users\\MINHAZULISLAM\\Desktop\\API Join\\Part1.ipt", matrix1)
    
    matrix2 = tg.CreateMatrix()
    translation_vector2 = tg.CreateVector(8 * 2.54, 6 * 2.54, 2 * 2.54)  # Converting inches to cm
    matrix2.SetTranslation(translation_vector2)
    occ2 = asmDef.Occurrences.Add("C:\\Users\\MINHAZULISLAM\\Desktop\\API Join\\Part2.ipt", matrix2)

    # Print the structure of the first occurrence
    print("Occurrence 1:")
    PrintStructure(occ1)
    print("\nOccurrence 2:")
    PrintStructure(occ2)

    # Get faces and edges by index
    face1 = occ1.SurfaceBodies.Item(1).Faces.Item(1)
    face2 = occ2.SurfaceBodies.Item(1).Faces.Item(1)
    edge1 = occ2.SurfaceBodies.Item(1).Edges.Item(1)
    edge3 = occ1.SurfaceBodies.Item(1).Edges.Item(1)

    # Debugging information
    if not face1:
        print("Face1 not found")
    if not face2:
        print("Face2 not found")
    if not edge1:
        print("Edge1 not found")
    if not edge3:
        print("Edge3 not found")

    # Check if the faces and edges were found
    if not face1 or not face2 or not edge1 or not edge3:
        raise ValueError("One or more named entities could not be found.")

    # Create geometry intents
    edge1Intent = asmDef.CreateGeometryIntent(edge1, 1)
    edge3Intent = asmDef.CreateGeometryIntent(edge3, 1)
    intentOne = asmDef.CreateGeometryIntent(face2, edge1Intent)
    intentTwo = asmDef.CreateGeometryIntent(face1, edge3Intent)

    # Debugging information for intents
    print("Edge1 Intent:", edge1Intent)
    print("Edge3 Intent:", edge3Intent)
    print("Intent One:", intentOne)
    print("Intent Two:", intentTwo)

    # Create a rotation joint between the two parts
    try:
        jointDef = asmDef.Joints.CreateAssemblyJointDefinition(102401, intentOne, intentTwo)  # kRotationalJointType
    except Exception as e:
        print("Error creating joint definition:", e)
        return

    jointDef.FlipAlignmentDirection = False
    jointDef.FlipOriginDirection = True
    joint = asmDef.Joints.Add(jointDef)

    # Make the joint visible
    joint.Visible = True

    # Drive the joint to animate it
    joint.DriveSettings.StartValue = "0 deg"
    joint.DriveSettings.EndValue = "180 deg"
    joint.DriveSettings.GoToStart()
    joint.DriveSettings.PlayForward()
    joint.DriveSettings.PlayReverse()

def PrintStructure(occurrence):
    # Print the structure of the occurrence
    partDef = occurrence.Definition
    surfaceBodies = partDef.SurfaceBodies
    print(f"Number of Surface Bodies: {surfaceBodies.Count}")
    for i in range(1, surfaceBodies.Count + 1):
        surfaceBody = surfaceBodies.Item(i)
        print(f"  Surface Body {i}:")
        print(f"    Number of Faces: {surfaceBody.Faces.Count}")
        for j in range(1, surfaceBody.Faces.Count + 1):
            print(f"      Face {j}")
        print(f"    Number of Edges: {surfaceBody.Edges.Count}")
        for k in range(1, surfaceBody.Edges.Count + 1):
            print(f"      Edge {k}")

# Execute the function
AssemblyJoint()

 

Labels (1)
2 REPLIES 2
Message 2 of 3
YuhanZhang
in reply to: 22351396

Try below updated code to check if the result is as expected:

 

import win32com.client

def AssemblyJoint():
    # Initialize Inventor application
    invApp = win32com.client.Dispatch("Inventor.Application")
    
    # Create a new assembly document
    asmDoc = invApp.Documents.Add(12291, invApp.FileManager.GetTemplateFile(12291, 8962))
    asmDef = asmDoc.ComponentDefinition
    tg = invApp.TransientGeometry
    
    # Place an occurrence into the assembly
    matrix1 = tg.CreateMatrix()
    translation_vector1 = tg.CreateVector(0, 0, 0)
    matrix1.SetTranslation(translation_vector1)
    occ1 = asmDef.Occurrences.Add("C:\\Users\\MINHAZULISLAM\\Desktop\\API Join\\Part1.ipt", matrix1)
    
    matrix2 = tg.CreateMatrix()
    translation_vector2 = tg.CreateVector(8 * 2.54, 6 * 2.54, 2 * 2.54)  # Converting inches to cm
    matrix2.SetTranslation(translation_vector2)
    occ2 = asmDef.Occurrences.Add("C:\\Users\\MINHAZULISLAM\\Desktop\\API Join\\Part2.ipt", matrix2)

    # Print the structure of the first occurrence
    print("Occurrence 1:")
    PrintStructure(occ1)
    print("\nOccurrence 2:")
    PrintStructure(occ2)

    # Get faces and edges by index
    face1 = occ1.SurfaceBodies.Item(1).Faces.Item(1)
    face2 = occ2.SurfaceBodies.Item(1).Faces.Item(1)
    edge1 = occ2.SurfaceBodies.Item(1).Edges.Item(1)
    edge3 = occ1.SurfaceBodies.Item(1).Edges.Item(1)

    # Debugging information
    if not face1:
        print("Face1 not found")
    if not face2:
        print("Face2 not found")
    if not edge1:
        print("Edge1 not found")
    if not edge3:
        print("Edge3 not found")

    # Check if the faces and edges were found
    if not face1 or not face2 or not edge1 or not edge3:
        raise ValueError("One or more named entities could not be found.")

    # Create geometry intents
    #edge1Intent = asmDef.CreateGeometryIntent(edge1, 1)
    edge3Intent = asmDef.CreateGeometryIntent(edge3, 1)
    
    intentOne = asmDef.CreateGeometryIntent(face2, 57867)
    intentTwo = asmDef.CreateGeometryIntent(face1, edge3.StopVertex.Point)

    # Debugging information for intents
    # print("Edge1 Intent:", edge1Intent)
    print("Edge3 Intent:", edge3Intent)
    print("Intent One:", intentOne)
    print("Intent Two:", intentTwo)

    # Create a rotation joint between the two parts
    try:
        jointDef = asmDef.Joints.CreateAssemblyJointDefinition(102401, intentOne, intentTwo)  # kRotationalJointType
    except Exception as e:
        print("Error creating joint definition:", e)
        return

    jointDef.FlipAlignmentDirection = False
    jointDef.FlipOriginDirection = True
    joint = asmDef.Joints.Add(jointDef)

    # Make the joint visible
    joint.Visible = True

    # Drive the joint to animate it
    joint.DriveSettings.StartValue = "0 deg"
    joint.DriveSettings.EndValue = "180 deg"
    joint.DriveSettings.GoToStart()
    joint.DriveSettings.PlayForward()
    joint.DriveSettings.PlayReverse()

def PrintStructure(occurrence):
    # Print the structure of the occurrence
    partDef = occurrence.Definition
    surfaceBodies = partDef.SurfaceBodies
    print(f"Number of Surface Bodies: {surfaceBodies.Count}")
    for i in range(1, surfaceBodies.Count + 1):
        surfaceBody = surfaceBodies.Item(i)
        print(f"  Surface Body {i}:")
        print(f"    Number of Faces: {surfaceBody.Faces.Count}")
        for j in range(1, surfaceBody.Faces.Count + 1):
            print(f"      Face {j}")
        print(f"    Number of Edges: {surfaceBody.Edges.Count}")
        for k in range(1, surfaceBody.Edges.Count + 1):
            print(f"      Edge {k}")

# Execute the function
AssemblyJoint()

 

You used a cylindrical Face to create a GeometryIntent, that you can below Intent values (from PointIntentEnum) to specify which point on the cylindrical axis will be used:

 

kAxisEndPointIntent 57867  
kAxisMidPointIntent 57866  
kAxisStartPointIntent 57865

 

Thanks,

Rocky



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

Message 3 of 3
22351396
in reply to: YuhanZhang

Thank You

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report