Object or with block variable not set Error when try to find Points in Occurence

Object or with block variable not set Error when try to find Points in Occurence

SebastianMK
Participant Participant
689 Views
6 Replies
Message 1 of 7

Object or with block variable not set Error when try to find Points in Occurence

SebastianMK
Participant
Participant

Hello all,

 

with much help from this forum and the AU, I'm delevoping an iLogic rule that creates dimensions between points on a idw.

 

I read the points from an excel table and run thru the Occurence of my Assembly to find the Points and add a linear dimension to those points.

 

However, this is working if all the points are found in my model. But if Points doesn't exists the rule throws me an Object or with block variable not set Error.

 

I use this Function to find the points in the Occurences and return the Occurence if a Point is found:

 

Private Function FindPoints(oView As DrawingView, dimPoint As String)

Dim oAssyDoc As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument
Dim occ As Object
Dim oDef As Document = Nothing

For Each oOcc As ComponentOccurrence In oAssyDoc.ComponentDefinition.Occurrences
oDef = oOcc.Definition.Document

For Each oWorkPoint As WorkPoint In oDef.ComponentDefinition.WorkPoints

       If oWorkPoint.Name = dimPoint Then
       occ = oOcc
Else

' It crashes here, when the point I'm looking for is not present in the model, I would just like the function to go on 'and check for the next point without throwing the error and stopping the rule
      Exit For

End If
Next
Next
Return occ
End Function

 

I would appreciate your help and any advice that points me to the solution.

Thanks

Sebastian

 

0 Likes
Accepted solutions (1)
690 Views
6 Replies
Replies (6)
Message 2 of 7

basautomationservices
Advocate
Advocate

 

 

In iLogic you can use try / catch / finally. (link) to catch errors.

 

Usually you are better off to actually prevent the error from being raised. You can check if the workpoint is nothing for example. 

Contact me for custom app development info@basautomationservices.com. Follow below links to view my Inventor appstore apps.

Free apps: Smart Leader | Part Visibility Utility | Mate Origins

Paid apps: Frame Stiffener Tool | Constrain Plane Toggle | Property Editor Pro


0 Likes
Message 3 of 7

WCrihfield
Mentor
Mentor

Hi @SebastianMK.  I see a couple of things that might potentially be problematic in your code.  I don't see an 'As ...' declaration at the end of your Function's definition line, which is a little odd.  That specifies what type of data or object it is designed to return.  The other thing is that you may need to filter out any virtual components or the components that just represent the welds in a weldment type assembly, while looping through the components.  Those will not have any WorkPoints, and therefore would throw an error when you attempt to access them.

What do you do with the component that the function returns?  I would assume you would attempt get the WorkPointProxy for the WorkPoint when you find it, then maybe use something like the DrawingView.ModelToDrawingViewSpace Method to get the 2D point on the drawing view for the dimension.  I don't think you can use the DrawingView.DrawingCurves property to be able to get drawing view geometry from a WorkPoint.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 7

Curtis_Waguespack
Consultant
Consultant

Hi @SebastianMK 

 

If I'm reading it correctly, I think you might be able to resolve this by changing the "Exit For" to "Continue For".

 

Edit to add:

Depending on how you are using the function, the continue for will catch the issue where some occurrences don't have the named point.

 

But you might still need to catch the situation when no occurrences have the point... again, though it depends on how you are using the function.

 

See example(s) below.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Sub main
	Dim oDoc As DrawingDocument
	oDoc = ThisDoc.Document
	Dim oView As DrawingView
	oView = oDoc.ActiveSheet.DrawingViews.Item(1)
	Dim OC As ComponentOccurrence

Try
	OC = FindPoints(oView, "TestPoint99")
	MsgBox(OC.Name)
Catch
End Try

End Sub

Private Function FindPoints(oView As DrawingView, dimPoint As String)

	Dim oAssyDoc As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument
	Dim occ As Object
	Dim oDef As Document = Nothing

	For Each oOcc As ComponentOccurrence In oAssyDoc.ComponentDefinition.Occurrences
		oDef = oOcc.Definition.Document

		For Each oWorkPoint As WorkPoint In oDef.ComponentDefinition.WorkPoints

			If oWorkPoint.Name = dimPoint Then
				occ = oOcc
			Else
				Continue For
			End If
		Next
	Next

	Return occ

End Function

 

EESignature

0 Likes
Message 5 of 7

SebastianMK
Participant
Participant
@Curtis_Waguespack , @WCrihfield , @basautomationservices 
Thank you so much for the replies.

With your help I was able to update the FindPoints Function
and detected that the issue and the error message comes from a different part :-)

Unfortunately I don't know what's the problem. I marked the section in red that delivers the error.
I think when the point not found the return is empty or nothing, so my check for Empty String fails.

I added the code maybe this helps.
I appreciate every hint to solve this issue 😓

Here is the code:

Sub Main

	Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument
	Dim dimNumber As Integer = 0
	Dim dimPt(1) As String
	Dim oOcc(1) As Object
	Dim viewName As String = String.Empty
	Dim sheetNumber As Integer = Nothing
	Dim dimType As String = String.Empty
	Dim dimTextX As Integer = Nothing
	Dim dimTextY As Integer = Nothing
	Dim tmpPointX As Integer
	Dim tmpPointY As Integer

	'*** Set Variables
	Dim cellVal As Object = Nothing
	Dim inc As Integer = 2

	'*** Make connection to Excel File
	Dim xVal As String = GoExcel.CellValue("C:\Users\cad\Downloads\Dimension.xlsx", "Belt", "A1")

	'*** Loop through Excel File Until empty row
	Do
		dimPt(0) = GoExcel.CellValue("A" & inc)
		dimPt(1) = GoExcel.CellValue("B" & inc)
		viewName = GoExcel.CellValue("C" & inc)
		sheetNumber = GoExcel.CellValue("D" & inc)
		dimType = GoExcel.CellValue("E" & inc)
		dimTextX = GoExcel.CellValue("F" & inc)
		dimTextY = GoExcel.CellValue("G" & inc)
		
		ActiveSheet = ThisDrawing.Sheet("Assembly drawing:" & sheetNumber)
		
		Dim oView As DrawingView = ActiveSheet.View(viewName).View
		Dim oSheet As Sheet = oDoc.Sheets.Item(sheetNumber)
	
	For count As Integer = 1 To 2
		Try
			oOcc(count - 1) = FindPoints(oView, dimPt(count - 1))
			MsgBox(oOcc(count - 1).name)			
		Catch
			MsgBox("Not found")
		End Try
	Next

	'here it fails, I think because if Point not found in the Function, the return value is Nothing or so...
	If oOcc(0).Name = String.Empty Or oOcc(1).Name = String.Empty Then
		MessageBox.Show("Points Could Not Be Found")
	Else
		'MsgBox("found a point")
		Call CreateLinearDimension(oDoc, oSheet, oView, dimPt(0), dimPt(1), oOcc(0), oOcc(1), dimType, dimTextX, dimTextY)
	End If	

	inc = inc + 1
	cellVal = GoExcel.CellValue("A" & inc)
	Loop Until (cellVal Is Nothing OrElse String.IsNullOrEmpty(cellVal.ToString()))

End Sub


Private Function FindPoints(oView As DrawingView, dimPoint As String)

	Dim oAssyDoc As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument
	Dim occ As Object
	Dim oDef As Document = Nothing

		For Each oOcc As ComponentOccurrence In oAssyDoc.ComponentDefinition.Occurrences
		oDef = oOcc.Definition.Document		
		'MsgBox(oOcc.Name)
		
			For Each oWorkPoint As WorkPoint In oDef.ComponentDefinition.WorkPoints		
				'MsgBox(oWorkPoint.Name)
				If oWorkPoint.Name = dimPoint Then
					occ = oOcc
				Else
					Continue For
				End If
			Next
		Next
		
	Return occ
End Function


 

0 Likes
Message 6 of 7

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @SebastianMK,

 

Try changing the problem line from 

 

If oOcc(0).Name = String.Empty Or oOcc(1).Name = String.Empty Then

     to

If oOcc(0) Is Nothing Or oOcc(1) Is Nothing Then

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

EESignature

0 Likes
Message 7 of 7

SebastianMK
Participant
Participant
Thanks Curtis,

this worked!
Appreciate your help!