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: 

Coordinate Calculator of Workpoints from Custom Origin

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
get2dpk
917 Views, 12 Replies

Coordinate Calculator of Workpoints from Custom Origin

Hi, 
I am trying to automatically pull the X,Y,Z Coordinates from  Custom Origin.
For Example,I  have an Custom UCS for which the X,Y,Z coordinates has to be measured from. 

I am getting correct values when measuring from Origin Center point. But for the custom UCS the Code generates the Wrong measurements as it doesn't sense the Vector Direction for the Positive and Negative Axis.

Can anyone please help to automate the above one?

I have attached picture and Output values.

I am new to ilogic so I would really appreciate for any help..

Labels (4)
12 REPLIES 12
Message 2 of 13
WCrihfield
in reply to: get2dpk

Hi @get2dpk.  It is fairly difficult to follow your code without fully understanding what all is going on in your model space and what all your intent was when creating these things.  Often when getting results that look like the bottom image in Picture4, it has to do with the Double data type, and not rounding results off to a reasonably short number of decimal places.  What values did you intend to see in that scenario?  Where is your custom UCS's origin in reference to the regular origin?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 13
get2dpk
in reply to: WCrihfield

hi  @WCrihfield , 

Thanks for the reply.
Please see below information for your Questions.

1.What all is going on in your model space and what all your intent was when creating these things.? - I am trying to automatically create a table listing X,Y,Z coordinates of workpoints  from a custom point as datum. For example   Usually In an Assembly/Part I will fix a datum point (not the same as Origin point) .  create Workpoints on Holes, Ports, and measure them from the Custom datum manually. 

2.It has to do with the Double data type, and not rounding results off to a reasonably short number of decimal place?- I have attached the Correct Values in the picture below. you can see  these values varies more than the double limit. I think it has to do something with the Inventor vector Direction . 

Capture2.JPG

3.Where is your custom UCS's origin in reference to the regular origin?-picture attached

Capture.JPG

 

Message 4 of 13
WCrihfield
in reply to: get2dpk

Hi @get2dpk. I had to create the model file for this scenario for myself, so I had something to test out my theories on. I just created the part, then used an iLogic rule to create the UCS on it at the same position and orientation as your model was. Then I wrote a new iLogic rule, from scratch, to analyze and measure the offsets between Origin & WorkPoints, and for the UCS origin to WorkPoints. My code works just fine, after a couple tweaks. I will post the results here, so they will hopefully help you figure things out the way you need them.  I attached the Part (2022 version), and attached text files for both iLogic rules I used (also within the part).

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 13
get2dpk
in reply to: WCrihfield

hi @WCrihfield 
Thanks for the Ilogic code. it works.😊

Still I see that User has to manually update the Code for name and the Instances of Workpoints. Here the code is written to 3 workpoints. so if we use 5 workpoints, we have to go to code and update  code  right?

get2dpk_0-1663996458879.png

Is there any way to measure all the available workpoints without user editing the code?  I meant  Inventor itself will recognize the number of Workpoints in the  model ?

please refer the below image from old ilogic rule .(the values are wrong though). I didnot specify the number of workpoints. is it possible to do something similar to this in the Ilogic rule you made .

 

get2dpk_1-1663996787851.png

get2dpk_2-1663996845186.png

2. I cannot open the Ipt model you shared since I am using 2020. Also the Ilogic rule  is not running for Assembly

get2dpk_3-1663997140847.png

 

 

Thanks in Advance!

Message 6 of 13
JelteDeJong
in reply to: get2dpk

If I understand correctly you want to get all work points relative to a UCS. And write the info to a csv file. In that case, try this iLogic rule. I changed the way the user needs to select the UCS. In my rule the user should select the UCS by clicking on it. In stead of selecting it from a list.

 

 

Sub Main()

	Dim doc As PartDocument = ThisDoc.Document
	Dim uom As UnitsOfMeasure = doc.UnitsOfMeasure
	Dim workPoints = doc.ComponentDefinition.WorkPoints
	Dim origin = workPoints.Item("Center Point")

	Dim ucs As UserCoordinateSystem = ThisApplication.CommandManager.Pick(
		SelectionFilterEnum.kUserCoordinateSystemFilter,
		"Select an UserCoordinateSystem")

	Dim translationVector = ucs.Origin.Point.VectorTo(origin.Point)

	Dim csvFileInfo As New IO.FileInfo(ThisDoc.PathAndFileName(False) & ".csv") 
	If csvFileInfo.Exists Then
		csvFileInfo.Delete()
	End If

	Using csvWriter As IO.StreamWriter = csvFileInfo.CreateText()
		' This will make sure Excel will reconise the column seperator
		csvWriter.WriteLine("sep=,") 
		csvWriter.WriteLine("Name,X,Y,Z")
		For Each workPoint As WorkPoint In workPoints
			If (WorkPoint.IsCoordinateSystemElement) Then Continue For

			Dim p As Point = WorkPoint.Point
			p.TranslateBy(translationVector)

			Dim x As Double = ConvertToInch(uom, p.X)
			Dim y As Double = ConvertToInch(uom, p.Y)
			Dim z As Double = ConvertToInch(uom, p.Z)
			csvWriter.WriteLine(string.Format("{0}, {1}, {2}, {3}",workPoint.Name,x,y,z))
		Next
	End Using
End Sub

Private Function ConvertToInch(uom As UnitsOfMeasure, value As Double) As Double
	value = uom.ConvertUnits(value, UnitsTypeEnum.kDatabaseLengthUnits, UnitsTypeEnum.kInchLengthUnits)
	Return Math.Round(value, 10)
End Function

 

 

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 7 of 13
get2dpk
in reply to: JelteDeJong

hi Jeltehjong,

 

Thanks for the  code! It works as I thought.😃

Could you Please help me with  some Changes in your code

 

1.It will be handy if the User can specify the Folder Location to store  the CSV File. so A Input Box for the Location will be great where user can specify the Folder while running the Code.

2.Also a Message pop up showing up  coordinate Values at the end of the Automation.

 

3.Is it possible to bring the UCS selection in list instead of selecting it from window.(not mandatory)

get2dpk_0-1664640681252.png

 

Appreciate your Help!

 

 

 

Message 8 of 13
JelteDeJong
in reply to: get2dpk

What about this:

Sub Main()

	Dim doc As PartDocument = ThisDoc.Document
	Dim uom As UnitsOfMeasure = doc.UnitsOfMeasure
	Dim workPoints = doc.ComponentDefinition.WorkPoints
	Dim origin = workPoints.Item("Center Point")

	Dim systems = doc.ComponentDefinition.UserCoordinateSystems
	Dim ucsNames = systems.Cast(Of UserCoordinateSystem).Select(Function(u) u.Name)
	Dim name = InputListBox("Pick one", ucsNames, ucsNames.First(), "Reference Origin", "Center Point and UCS")
	Dim ucs As UserCoordinateSystem = systems.Item(name)

	Dim translationVector = ucs.Origin.Point.VectorTo(origin.Point)

	Dim fileDialog As FileDialog
	ThisApplication.CreateFileDialog(fileDialog)
	fileDialog.DialogTitle = "Save points"
	fileDialog.Filter = "Point Files (*.csv)|*.csv"
	fileDialog.ShowSave()

	Dim csvFileInfo As New IO.FileInfo(fileDialog.FileName)
	If csvFileInfo.Exists Then
		csvFileInfo.Delete()
	End If

	Using csvWriter As IO.StreamWriter = csvFileInfo.CreateText()
		' This will make sure Excel will reconise the column seperator
		csvWriter.WriteLine("sep=,")
		csvWriter.WriteLine("Name,X,Y,Z")
		For Each workPoint As WorkPoint In workPoints
			If (WorkPoint.IsCoordinateSystemElement) Then Continue For

			Dim p As Point = WorkPoint.Point
			p.TranslateBy(translationVector)

			Dim x As Double = ConvertToInch(uom, p.X)
			Dim y As Double = ConvertToInch(uom, p.Y)
			Dim z As Double = ConvertToInch(uom, p.Z)
			csvWriter.WriteLine(String.Format("{0}, {1}, {2}, {3}", WorkPoint.Name, x, y, z))
		Next
	End Using
End Sub

Private Function ConvertToInch(uom As UnitsOfMeasure, value As Double) As Double
	value = uom.ConvertUnits(value, UnitsTypeEnum.kDatabaseLengthUnits, UnitsTypeEnum.kInchLengthUnits)
	Return Math.Round(value, 10)
End Function

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 9 of 13
get2dpk
in reply to: JelteDeJong

hi @JelteDeJong 

 

Yes, this is exactly what I wanted. Thanks a lot !😃

 

Message 10 of 13
get2dpk
in reply to: get2dpk

Hi @JelteDeJong 

 

I have been trying your Code and observed the Below issue

 

I  see that the 2nd and 5th Column(Yellow Highlighted) Values are decimal numbers after X and Z Coordinates respectively

For Eg

-the Actual  X coordinate dimension  of WP2  from UCS1 is -51.52 , but it is splitting itself into two Columns

-the Actual  Z coordinate dimension  of WP2 from UCS1 is  -11.395 , but it is splitting itself into two Columns

 

get2dpk_1-1667300780722.png

 

get2dpk_0-1667300739028.png

 

Could you please help in Correcting this Issue

appreciate your Help

 

Regards

Pradeep S

 

Message 11 of 13
WCrihfield
in reply to: get2dpk

Hi @get2dpk.  This is just an educated guess on my part, but since you are storing the values to a CSV (Comma Separated Values) file, and your numerical values use a comma as their decimal separator, instead of a period or dot, the Integer portion and decimal portion are being separated as separate values.  You may have to format your values differently if you want to store them to a CSV file, or store them in something other than a CSV file.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 12 of 13
JelteDeJong
in reply to: WCrihfield

I Expect that @WCrihfield is right. If you change the separator in the csv file to ; the problem is probably solved. try something like this:

Sub Main()

	Dim doc As PartDocument = ThisDoc.Document
	Dim uom As UnitsOfMeasure = doc.UnitsOfMeasure
	Dim workPoints = doc.ComponentDefinition.WorkPoints
	Dim origin = workPoints.Item("Center Point")

	Dim systems = doc.ComponentDefinition.UserCoordinateSystems
	Dim ucsNames = systems.Cast(Of UserCoordinateSystem).Select(Function(u) u.Name)
	Dim name = InputListBox("Pick one", ucsNames, ucsNames.First(), "Reference Origin", "Center Point and UCS")
	Dim ucs As UserCoordinateSystem = systems.Item(name)

	Dim translationVector = ucs.Origin.Point.VectorTo(origin.Point)

	Dim fileDialog As FileDialog
	ThisApplication.CreateFileDialog(fileDialog)
	fileDialog.DialogTitle = "Save points"
	fileDialog.Filter = "Point Files (*.csv)|*.csv"
	fileDialog.ShowSave()

	Dim csvFileInfo As New IO.FileInfo(fileDialog.FileName)
	If csvFileInfo.Exists Then
		csvFileInfo.Delete()
	End If

	Using csvWriter As IO.StreamWriter = csvFileInfo.CreateText()
		' This will make sure Excel will reconise the column seperator
		csvWriter.WriteLine("sep=;")
		csvWriter.WriteLine("Name;X;Y;Z")
		For Each workPoint As WorkPoint In workPoints
			If (workPoint.IsCoordinateSystemElement) Then Continue For

			Dim p As Point = workPoint.Point
			p.TranslateBy(translationVector)

			Dim x As Double = ConvertToInch(uom, p.X)
			Dim y As Double = ConvertToInch(uom, p.Y)
			Dim z As Double = ConvertToInch(uom, p.Z)
			csvWriter.WriteLine(String.Format("{0};{1};{2};{3}", workPoint.Name, x, y, z))
		Next
	End Using
End Sub

Private Function ConvertToInch(uom As UnitsOfMeasure, value As Double) As Double
	value = uom.ConvertUnits(value, UnitsTypeEnum.kDatabaseLengthUnits, UnitsTypeEnum.kInchLengthUnits)
	Return Math.Round(value, 10)
End Function

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 13 of 13

Hello ,

I'm inventor user.

I wan't to create a macro or rule ilogic for obtain z position for each point .

For obtain X and Y position I use a hole table by cliking to the centermark .

My UCS is costumize.

I join you some capture

mmsiamadouly_0-1722611441237.png

Thank to help me please.

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

Post to forums  

Autodesk Design & Make Report