AddFlushConstraint doesnt work as expected

AddFlushConstraint doesnt work as expected

Mehmet_Fatih_Turker
Advocate Advocate
339 Views
4 Replies
Message 1 of 5

AddFlushConstraint doesnt work as expected

Mehmet_Fatih_Turker
Advocate
Advocate
Dim oAssDoc As Inventor.AssemblyDocument = ThisApplication.ActiveDocument  'CType(oActiveDoc, AssemblyDocument)
Dim oAssyDef As Inventor.AssemblyComponentDefinition = oAssDoc.ComponentDefinition
Dim defaultMatrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix()
Dim midLegtoAssy As String = workspacePath & "\xxx\xxxxx1.ipt"
Dim sideLegstoAssy As String = workspacePath & "\xxx\xxxxx2.ipt"
Dim midLeg As Inventor.ComponentOccurrence = oAssyDef.Occurrences.Add(midLegtoAssy, defaultMatrix)
Dim midLegPartDoc As Inventor.PartDocument = midLeg.Definition.Document

Dim upLeg As Inventor.ComponentOccurrence = oAssyDef.Occurrences.Add(upanddownLegstoAssy, defaultMatrix)
Dim upLegPartDoc As Inventor.PartDocument = upLeg.Definition.Document

For Each wp As WorkPlane In midLegPartDoc.ComponentDefinition.WorkPlanes
	
	Logger.Trace("midLeg WorkPlane: " & wp.Name)
	
Next

For Each wp As WorkPlane In upLegPartDoc.ComponentDefinition.WorkPlanes
	
    Logger.Trace("upLeg WorkPlane: " & wp.Name)
	
Next
Dim midLegPlane As WorkPlane = midLegPartDoc.ComponentDefinition.WorkPlanes.Item("XZ Plane")
Dim upLegPlane As WorkPlane = upLegPartDoc.ComponentDefinition.WorkPlanes.Item("XZ Plane")
Dim o As Double = 0.0

If midLegPlane Is Nothing OrElse upLegPlane Is Nothing Then
	
	MessageBox.Show("Failed to retrieve one of the XZ planes.")
	
End If

Dim olf As FlushConstraint = oAssyDef.Constraints.AddFlushConstraint(midLegPlane , upLegPlane , 0)




 

Hi folks !

above I shared a part of my code that doesnt work so far. I was trying to (in a vary basic way) create flush constrain by bringing parts to assy then selecting workplanes then constraint it. it always giving me error for the line where my constraint described. why it doesnt work you think ?

0 Likes
Accepted solutions (2)
340 Views
4 Replies
Replies (4)
Message 2 of 5

Michael.Navara
Advisor
Advisor
Accepted solution

You need to use WorkPlaneProxy instead of WorkPlane for mate creation in assembly. See the code below.

 

Dim oAssDoc As Inventor.AssemblyDocument = ThisApplication.ActiveDocument  'CType(oActiveDoc, AssemblyDocument)
Dim oAssyDef As Inventor.AssemblyComponentDefinition = oAssDoc.ComponentDefinition
Dim defaultMatrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix()
Dim midLegtoAssy As String = workspacePath & "\xxx\xxxxx1.ipt"
Dim sideLegstoAssy As String = workspacePath & "\xxx\xxxxx2.ipt"
Dim midLeg As Inventor.ComponentOccurrence = oAssyDef.Occurrences.Add(midLegtoAssy, defaultMatrix)
Dim midLegPartDoc As Inventor.PartDocument = midLeg.Definition.Document

Dim upLeg As Inventor.ComponentOccurrence = oAssyDef.Occurrences.Add(upanddownLegstoAssy, defaultMatrix)
Dim upLegPartDoc As Inventor.PartDocument = upLeg.Definition.Document

For Each wp As WorkPlane In midLegPartDoc.ComponentDefinition.WorkPlanes

    Logger.Trace("midLeg WorkPlane: " & wp.Name)

Next

For Each wp As WorkPlane In upLegPartDoc.ComponentDefinition.WorkPlanes

    Logger.Trace("upLeg WorkPlane: " & wp.Name)

Next
Dim midLegPlane As WorkPlane = midLegPartDoc.ComponentDefinition.WorkPlanes.Item("XZ Plane")
Dim upLegPlane As WorkPlane = upLegPartDoc.ComponentDefinition.WorkPlanes.Item("XZ Plane")

'Create WorkPlaneProxy
Dim midLegPlaneProxy As WorkPlaneProxy
Dim upLegPlaneProxy As WorkPlaneProxy

midLeg.CreateGeometryProxy(midLegPlane, midLegPlaneProxy)
upLeg.CreateGeometryProxy(upLegPlane, upLegPlaneProxy)

Dim o As Double = 0.0

If midLegPlane Is Nothing OrElse upLegPlane Is Nothing Then

    MessageBox.Show("Failed to retrieve one of the XZ planes.")

End If

'Use WorkPlaneProxy instead WorkPlane
Dim olf As FlushConstraint = oAssyDef.Constraints.AddFlushConstraint(midLegPlaneProxy, upLegPlaneProxy, 0)

 

0 Likes
Message 3 of 5

Mehmet_Fatih_Turker
Advocate
Advocate

@Michael.Navara  hell yeah ! this worked quite well but another question rises.. I was preparing some snippets in order to see how iLogic behaves before starting actual coding. 

AddReference "System.Drawing"
AddReference "Microsoft.Office.Interop.Excel"
Option Explicit On
Option Infer Off
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
Imports Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices
Imports System.Linq
Imports System.Collections.Generic
Imports System.IO
Imports Autodesk.iLogic.Interfaces
Imports Microsoft.VisualBasic.VBCodeProvider
Sub Main()
	
Dim partPath1 As String = "\\1.2.3.4\asd\asd\asd\asd\asd\asd\asd\Workspace\asdasd1.ipt"
Dim partPath2 As String = "\\1.2.3.4\asd\asd\asd\asd\asd\asd\asd\Workspace\asdasd2.ipt"
Dim partPath3 As String = "\\1.2.3.4\asd\asd\asd\asd\asd\asd\asd\Workspace\asdasd.ipt"

' Create the parts
CreatePart(partPath1, "Part1")
CreatePart(partPath2, "Part2")
CreatePart(partPath3, "Part3")

' Insert the parts into the current assembly
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oCompDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
			
Dim part1 As ComponentOccurrence = oCompDef.Occurrences.Add(partPath1, ThisApplication.TransientGeometry.CreateMatrix())
Dim part1doc As PartComponentDefinition = part1.Definition
Dim part2 As ComponentOccurrence = oCompDef.Occurrences.Add(partPath2, ThisApplication.TransientGeometry.CreateMatrix())
Dim part2doc As PartComponentDefinition = part2.Definition
Dim part3 As ComponentOccurrence = oCompDef.Occurrences.Add(partPath3, ThisApplication.TransientGeometry.CreateMatrix())
Dim part3doc As PartDocument = part3.Definition.Document

' Mate face of Part1 with Part2
Dim part1Face As Face = part1.SurfaceBodies.Item(1).Faces.Item(1)
Dim part2Face As Face = part2.SurfaceBodies.Item(1).Faces.Item(1)
Dim part3Face As Face = part3.SurfaceBodies.Item(1).Faces.Item(1)
Dim part1workplane As WorkPlane = part1doc.WorkPlanes.Item(1)
Dim part2workplane As WorkPlane = part2doc.WorkPlanes.Item(1)

oCompDef.Constraints.AddFlushConstraint(part1Face , part3Face , 0)

oCompDef.Constraints.AddMateConstraint(part1Face, part2Face, 0, InferredTypeEnum.kNoInference, InferredTypeEnum.kNoInference)

oCompDef.Constraints.AddFlushConstraint(part2Face, part3Face, 0)

oAsmDoc.Update()
End Sub
' Function to create a new part
Sub CreatePart(partPath As String, partName As String)
    ' Check if the part already exists
    If Not System.IO.File.Exists(partPath) Then
        ' Create a new part
        Dim oPartDoc As PartDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject, ThisApplication.FileOptions.TemplatesPath & "\Standard.ipt", True)
        oPartDoc.DisplayName = partName

        ' Optionally, add geometry to the part (extrusions, sketches, etc.)
        Dim oCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
        Dim oSketch As PlanarSketch = oCompDef.Sketches.Add(oCompDef.WorkPlanes.Item(3)) ' Create sketch on WorkPlane(3)
        Dim tg As TransientGeometry = ThisApplication.TransientGeometry

        ' Create a rectangle for the sketch
        Dim centerPoint As Point2d = tg.CreatePoint2d(0, 0) ' Center point for the rectangle
        Dim cornerPoint As Point2d = tg.CreatePoint2d(10, 10) ' Corner point for the rectangle
        oSketch.SketchLines.AddAsTwoPointRectangle(centerPoint, cornerPoint)

        ' Create a profile from the sketch
        Dim oProfile As Profile = oSketch.Profiles.AddForSolid() ' Create a profile from the sketch
        
        ' Try to create an extrusion with the profile
        Try
            ' Create the extrusion definition
            Dim asdExtrudeDefinition As ExtrudeDefinition = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oSketch.Profiles.AddForSolid(), PartFeatureOperationEnum.kJoinOperation)

            asdExtrudeDefinition.SetDistanceExtent(50, PartFeatureExtentDirectionEnum.kSymmetricExtentDirection)' Set the extrusion distance

            Dim oExtrusion As ExtrudeFeature = oCompDef.Features.ExtrudeFeatures.Add(asdExtrudeDefinition)' Add the extrusion
        Catch ex As Exception
            MsgBox("Error during extrusion: " & ex.Message)
            Exit Sub
        End Try

        oPartDoc.SaveAs(partPath, False) ' Save the part
        oPartDoc.Close(True)
    End If
	End Sub



in here this one could give me flush constraint result. why would it work in here not the other ?

0 Likes
Message 4 of 5

Michael.Navara
Advisor
Advisor
Accepted solution

In this case it can work, because you obtain faces from occurrence. In this case you don't get Face but FaceProxy. Because FaceProxy is derived from Face, you can assign part2Face. This is not true for WorkPlanes. In this case you need to create WorkPlaneProxy object for corresponding occurrence.

 

 

Dim part2 As ComponentOccurrence = ...

Dim part2Face As Face = part2.SurfaceBodies.Item(1).Faces.Item(1) 'Returns FaceProxy

 

 

Message 5 of 5

Mehmet_Fatih_Turker
Advocate
Advocate

Thats perfect, thanks !

0 Likes