Context with .GetMinimumDistance throws error in VB.NET , not in VBA

Context with .GetMinimumDistance throws error in VB.NET , not in VBA

frederic.vandenplas
Collaborator Collaborator
268 Views
3 Replies
Message 1 of 4

Context with .GetMinimumDistance throws error in VB.NET , not in VBA

frederic.vandenplas
Collaborator
Collaborator

Hi,

 

With the code below i do the following:

 

Loop in part 1 which is in a subassembly through all circular holes

Measure the shortest distance to...

Loop in part 2 which is in a subassembly through all faces of a specific body

Then i check if the holes are not to close with GetMinimumDistance

 

All works fine, but oContext throws an error in VB.NET although the same code works in VBA

In need the oContext to calculate the Shortest distance in the XY-Plane, because shortest distance gives the shortest distance in 3 directions.

Other solutions are welcome! 

 

 

 

Sub CheckHolesAgainstGlass()
    Try
        Dim oOccurenceGlas As ComponentOccurrence = Nothing
        Dim oOccurenceBuitenvel As ComponentOccurrence = Nothing
        Dim oPartCompDefGlas As PartComponentDefinition
        Dim oPartCompDefBuitenvel As PartComponentDefinition
        Dim oBodyGlas As SurfaceBody = Nothing
        Dim oBodyBuitenvel As SurfaceBody = Nothing

        For Each oRefAssemblyDoc As Document In oAsmdoc.ReferencedDocuments
            Try
                If oRefAssemblyDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
                    Dim oRefAsmDoc As AssemblyDocument = oRefAssemblyDoc
                    Dim oRefAsmDocDef As AssemblyComponentDefinition = oRefAsmDoc.ComponentDefinition
                    Select Case IO.Path.GetFileName(oRefAsmDoc.FullFileName)
                        Case "Assembly Buitenvel Magnetude.iam"
                            oOccurenceBuitenvel = oRefAsmDocDef.Occurrences(1)
                            oPartCompDefBuitenvel = oOccurenceBuitenvel.Definition
                            oBodyBuitenvel = oPartCompDefBuitenvel.SurfaceBodies(1)
                        Case "Assembly Trekkers.iam"
                            oOccurenceGlas = oRefAsmDocDef.Occurrences(1)
                            oPartCompDefGlas = oOccurenceGlas.Definition
                            oBodyGlas = oPartCompDefGlas.SurfaceBodies(2)
                    End Select
                End If
            Catch
            End Try
        Next oRefAssemblyDoc

        For Each oFaceBuitenvel As Face In oBodyBuitenvel.Faces
            If oFaceBuitenvel.SurfaceType = SurfaceTypeEnum.kCylinderSurface And oFaceBuitenvel.Edges.Count = 2 Then
                Dim FaceProxyBuitenvel As FaceProxy = Nothing
                oOccurenceBuitenvel.CreateGeometryProxy(oFaceBuitenvel, FaceProxyBuitenvel)
                Dim FaceProxyGlas As FaceProxy = Nothing
                For Each oFaceGlas As Face In oBodyGlas.Faces

                    Call oOccurenceGlas.CreateGeometryProxy(oFaceGlas, FaceProxyGlas)
Dim oContext As NameValueMap '= InvApp.TransientObjects.CreateNameValueMap()
                    InvApp.MeasureTools.GetMinimumDistance(FaceProxyGlas, FaceProxyBuitenvel, InferredTypeEnum.kNoInference, InferredTypeEnum.kNoInference, oContext)
                    Dim dblDistance As Double = Math.Sqrt((oContext.Item(2).X - oContext.Item(3).X) ^ 2 + (oContext.Item(2).Y - oContext.Item(3).Y) ^ 2) * 10
                    If dblDistance < 50 Then
                        SendMail("Distance to small :" & Round(dblDistance, 1).ToString)
                        Exit Sub
                    End If

                Next oFaceGlas

            End If
        Next
    Catch ex As Exception

    End Try

End Sub

 

 

 

 

If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"
0 Likes
269 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor

Hi @frederic.vandenplas.  A situation like this may be possible for someone else to figure out, without having your file set to test with, but a couple things come to mind.  The things I have in mind though, may not explain why it would be working in VBA, but not in vb.net.

 

For one thing, I believe that you are supposed to 'create' the NameValueMap for the 'Context', then supply that to the method, not just declare a variable for one.  The online documentation for the iLogic & Inventor API stuff is notoriously bad about indicating whether specific 'input' parameters are supposed to be 'instantiated' before supplying them, or if that object will just magically be created for us, but in most cases where it may seem questionable, but usually creating one, then supplying it is the intended way.

 

The other thing that comes to mind is a different type of 'context' (3D coordinate system of a ComponentDefinition), but it is difficult to tell remotely if this is an issue here or not.  Basically, not all 'proxies' are created equal.  What I mean is, if your intentions are to measure the minimum distance between two faces in the context of the main assembly, then you must first get a reference to the two FaceProxy objects that only exist within the context of that top level assembly, but if those two components are parts down within different sub assemblies, then you will need to step up the proxy ladder at least 2 steps, not just one.  The FaceProxy objects you are getting are still in the context of those sub assemblies, because those part components were within those sub assemblies, not top level components, so they are not yet in the context of the main assembly.  In order to get the FaceProxy objects that exist in the context of the top level assembly, you will have to use the component objects for the part component's parent sub assemblies, then use those to make a proxy of the original proxies.  Then those new proxy references will be in the context of those sub assembly component's parent assembly, which I assume is the top level assembly.  If those part components are down deeper than within top level sub assemblies, then you will have more steps up the proxy ladder to go to get to the top.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 4

frederic.vandenplas
Collaborator
Collaborator

@WCrihfield 

Thank you for your opinion, i think that i indeed need to level up the proxies, i'll try doing that based on some topics that i found based on your suggestion!

https://adndevblog.typepad.com/manufacturing/2013/07/occurrences-contexts-definitions-proxies.html

https://adndevblog.typepad.com/manufacturing/2012/12/componentoccurrence-contexts-and-reference-keys...

If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"
0 Likes
Message 4 of 4

cadconfigurator
Participant
Participant

Hi, @WCrihfield 

You pointed me towards the solution, it's indeed all about Context, because i've created the proxies in 2 different parts or assemblies, the measuretool could not provide context, now my context only exists in the main assembly and the code below works, now i can optimize and implement it.

Thanks!

Sub Main()

Dim invapp As Inventor.Application = ThisApplication
Dim oAsmDoc As AssemblyDocument = invapp.ActiveDocument
Dim oAsmDocDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition

Dim oOccurenceGlas As ComponentOccurrence = Nothing
Dim oOccurenceBuitenvel As ComponentOccurrence = Nothing
Dim oPartCompDefGlas As PartComponentDefinition
Dim oPartCompDefBuitenvel As PartComponentDefinition
Dim oBodyGlas As SurfaceBody = Nothing
Dim oBodyBuitenvel As SurfaceBody = Nothing

For Each oRefAssemblyDoc As Document In oAsmdoc.ReferencedDocuments
	Try
		If oRefAssemblyDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			Dim oRefAsmDoc As AssemblyDocument = oRefAssemblyDoc
			Dim oRefAsmDocDef As AssemblyComponentDefinition = oRefAsmDoc.ComponentDefinition
			Select Case IO.Path.GetFileName(oRefAsmDoc.FullFileName)
				Case "Assembly Buitenvel Magnetude.iam"
					oOccurenceBuitenvel = oRefAsmDocDef.Occurrences(1)
					oPartCompDefBuitenvel = oOccurenceBuitenvel.Definition
					oBodyBuitenvel = oPartCompDefBuitenvel.SurfaceBodies(1)
					Logger.Info(oBodyBuitenvel.Name)
				Case "Assembly Glas.iam"
					oOccurenceGlas = oRefAsmDocDef.Occurrences(1)
					oPartCompDefGlas = oOccurenceGlas.Definition
					oBodyGlas = oPartCompDefGlas.SurfaceBodies(2)
					Logger.Info(oBodyGlas.Name)
			End Select
		End If
	Catch
	End Try
Next oRefAssemblyDoc

For Each oFaceBuitenvel As FaceProxy In oAsmDocDef.Occurrences(1).SubOccurrences(1).SurfaceBodies(1).Faces

	If oFaceBuitenvel.SurfaceType = SurfaceTypeEnum.kCylinderSurface And oFaceBuitenvel.Edges.Count = 2 Then
		For Each oFaceGlas As FaceProxy In oAsmDocDef.Occurrences(2).SubOccurrences(1).SurfaceBodies(2).Faces
			Dim oContext As NameValueMap' = invapp.TransientObjects.CreateNameValueMap

			invapp.MeasureTools.GetMinimumDistance(oFaceGlas, oFaceBuitenvel, InferredTypeEnum.kNoInference, InferredTypeEnum.kNoInference, oContext)
			Dim dblDistance As Double = Math.Sqrt((oContext.Item(2).X - oContext.Item(3).X) ^ 2 + (oContext.Item(2).Y - oContext.Item(3).Y) ^ 2) * 10
			If dblDistance < 50 Then
				Logger.Info("Afstand te klein " & Round(dblDistance, 1).ToString)
			Else
				Logger.Info("Afstand OK " & Round(dblDistance, 1).ToString)
			End If
		Next oFaceBuitenvel

	End If
Next


End Sub

 

0 Likes