Automatic centermark alignment

Automatic centermark alignment

jwoodhouseD8XAE
Contributor Contributor
4,901 Views
14 Replies
Message 1 of 15

Automatic centermark alignment

jwoodhouseD8XAE
Contributor
Contributor

I'm trying to create a script to align all the centermarks to the center of the view. I have everything working except for the actual alignment of the centermarks. The line that says "oMark.ExtensionPointOne = oPoint" doesn't do anything after I checked it with some message boxes. Any help would be greatly appreciated as this is something I have to do on every sheet of every drawing I make.

0 Likes
4,902 Views
14 Replies
Replies (14)
Message 2 of 15

WCrihfield
Mentor
Mentor

Hi @jwoodhouseD8XAE.  When I looked at the Centermark.ExtensionPointOne online help description, I saw that it may not have a value if the Centermark.ExtensionLinesVisible property's value is False, or if the extension line is 'clipped off' by a broken view'.  So setting that ExtensionLinesVisible property to True first may be needed.  Not sure, but whether that property's value will naturally be True or False may also be dictated by the active 'Style' you used (or are using) in the drawing also.

When I quickly reviewed your code, it seemed like the following line of code may not be working correctly, but I may be wrong:

Dim angle As Double = Math.Atan2(dy, dx) ' Calculate angle in radians

...because I don't see where those two variables "dy" & "dx" have been created or values set to them.  If so, then your "angle" variable may have an incorrect value.  Other than that, I have not actually tested the code myself yet.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 15

jwoodhouseD8XAE
Contributor
Contributor

I forgot to paste in the "dx" and "dy" definitions into the .txt file. They're in there now. I added a line to set the Centermark.ExtensionLinesVisible property to True and the issue persists. I've tested and I know that oPoint is correctly translated to the new position, but it seems like oMark.ExtensionPointOne is being set incorrectly or just not the correct way to do this, but I'm not really sure what else to try. 

0 Likes
Message 4 of 15

jwoodhouseD8XAE
Contributor
Contributor

This is not the correct way to do this. I just ran this script on a part with larger holes and all it does is changes where the extension line ends, but it is only affected by the change in the direction of the extension line. So if the extension line is only in the x- direction, the new position only affects the x position. See attached picture. Any help for how to rotate these centermarks would be greatly appreciated.

jwoodhouseD8XAE_0-1739557465982.png

 

0 Likes
Message 5 of 15

WCrihfield
Mentor
Mentor

I agree.  I was just modifying a copy of your code locally, to optimize it a bit in a couple places, and was realizing the same thing.  This is partly because the properties like Centermark.ExtensionPointOneDirection are ReadOnly, and so is the Centermark.Centerlines property.  So, it seems like we can turn the extension lines on & off, and can move their end points, but can not rotate their direction.  It seems like the only way I have seen to get the Centermarks to point towards the center of a view is if the part itself is round, and the holes in the part are associated with a circular pattern, which it can recognize when using the DrawingView.SetAutomatedCenterlineSettings method, once you have set-up the AutomatedCenterlineSettings object the way you want it.

 

By the way, below are just a couple snippets I was going to suggest as changes to your original code that you might have liked.

'get view bounds box (for checking if Centermark location is within later)
Dim oViewBox As Inventor.Box2d = oTG.CreateBox2d()
oViewBox.MinPoint = oTG.CreatePoint2d(oView.Left, oView.Top - oView.Height)
oViewBox.MaxPoint = oTG.CreatePoint2d(oView.Left + oView.Width, oView.Top)

...then a few lines later, while iterating the Centermarks

If oViewBox.Contains(oMark.Position) Then

...followed by another 'angle' I was working on

'get direction and distance from Centermark's position to view's center
Dim oVect As Inventor.Vector = oMark.Position.VectorTo(oViewCenter)

...but that's when I realized that those other properties were ReadOnly, and saw your reply also, so I stopped there.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 15

jwoodhouseD8XAE
Contributor
Contributor

Unfortunately, not all holes are on patterns. I appreciate your help though and I'll use those code snippets to clean up the code a little bit. I'll keep digging and update this post if I figure anything out.

0 Likes
Message 7 of 15

J-Camper
Advisor
Advisor

@jwoodhouseD8XAE,

Try this:

' Get the active drawing document
Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim oSheet As Sheet = oDoc.ActiveSheet
Dim count As Integer = 0
Dim tg As TransientGeometry = ThisApplication.TransientGeometry
' Loop through each view on the sheet
For Each oView As DrawingView In oSheet.DrawingViews
    'Get the center of the view
    Dim oViewCenter As Point2d = oView.Center'.Copy
	'Get Range Of View
	Dim oViewMin As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oViewCenter.X - oView.Width/2, oViewCenter.Y - oView.Height/2)
    Dim oViewMax As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oViewCenter.X + oView.Width/2, oViewCenter.Y + oView.Height/2)

    ' Loop through all centermarks in the sheet
    For Each oMark As Centermark In oSheet.Centermarks

        Dim oMarkPos As Point2d = oMark.Position
		'make sure centermark is associated with current view
		If (oMarkPos.X >= oViewMin.X And oMarkPos.X <= oViewMax.X) And (oMarkPos.Y >= oViewMin.Y And oMarkPos.Y <= oViewMax.Y) Then
			oMark.ExtensionLinesVisible = True
			'Get test Vectors
		   	Dim TowardsCenterX As Vector2d = tg.CreatePoint2d(oMarkPos.X, oViewCenter.Y).VectorTo(oViewCenter)
			Dim TowardsCenterY As Vector2d = tg.CreatePoint2d(oViewCenter.X, oMarkPos.Y).VectorTo(oViewCenter)
			'Adjust Centermark in X Direction
			If TowardsCenterX.Length <> 0
				Select Case True
					Case oMark.ExtensionPointOneDirection.IsEqualTo(TowardsCenterX.AsUnitVector)
						oMark.ExtensionPointOne = tg.CreatePoint2d(oViewCenter.X, oMark.ExtensionPointOne.Y)
					Case oMark.ExtensionPointTwoDirection.IsEqualTo(TowardsCenterX.AsUnitVector)
						oMark.ExtensionPointTwo = tg.CreatePoint2d(oViewCenter.X, oMark.ExtensionPointTwo.Y)
					Case oMark.ExtensionPointThreeDirection.IsEqualTo(TowardsCenterX.AsUnitVector)
						oMark.ExtensionPointThree = tg.CreatePoint2d(oViewCenter.X, oMark.ExtensionPointThree.Y)
					Case oMark.ExtensionPointFourDirection.IsEqualTo(TowardsCenterX.AsUnitVector)
						oMark.ExtensionPointFour = tg.CreatePoint2d(oViewCenter.X, oMark.ExtensionPointFor.Y)
				End Select
			End If
			'Adjust Centermark in Y Direction
			If TowardsCenterY.Length <> 0
				Select Case True
					Case oMark.ExtensionPointOneDirection.IsEqualTo(TowardsCenterY.AsUnitVector)
						oMark.ExtensionPointOne = tg.CreatePoint2d(oMark.ExtensionPointOne.X, oViewCenter.Y)
					Case oMark.ExtensionPointTwoDirection.IsEqualTo(TowardsCenterY.AsUnitVector)
						oMark.ExtensionPointTwo = tg.CreatePoint2d(oMark.ExtensionPointTwo.X, oViewCenter.Y)
					Case oMark.ExtensionPointThreeDirection.IsEqualTo(TowardsCenterY.AsUnitVector)
						oMark.ExtensionPointThree = tg.CreatePoint2d(oMark.ExtensionPointThree.X, oViewCenter.Y)
					Case oMark.ExtensionPointFourDirection.IsEqualTo(TowardsCenterY.AsUnitVector)
						oMark.ExtensionPointFour = tg.CreatePoint2d(oMark.ExtensionPointFour.X, oViewCenter.Y)
				End Select
			End If
			
		End If

    Next

Next

oDoc.Update()

 

I changed how to determine which direction needs to move to align it with view center.  It worked on my small test case, but let me know if you have any issues or questions

0 Likes
Message 8 of 15

jwoodhouseD8XAE
Contributor
Contributor

That's not quite what I'm trying to do. I'm wanting to rotate the centermarks so that one of the extension lines is pointing towards the center, they don't have to extend to the center, they just need to be oriented. At least when I tried this code all it did was extend the extension lines of each centermark to either the x-axis or y-axis of the view. 

 

For clarity this is our current process for aligning the centermarks that I'm trying to automate:

1. create a sketch on the view and draw a line from the center of the view to the center of each hole

2. exit the sketch and select each centermark individually, right click and hit "edit -> align to edge" then select the line corresponding to that hole

 

This is made more efficient by recognizing patterns of 2 or 4 and using the same line for multiple holes when possible, but ideally this would all be automated by this rule.

0 Likes
Message 9 of 15

J-Camper
Advisor
Advisor

I see.  Unfortunately i don't see an "align to edge" method available, and when i try to set an extension point that is not in the direction of the extension point direction, nothing changes.  The extension point direction is read only so we can't just set it to be whatever we want.  I don't even see an option to specify direction when creating a new centermark.  There is a control definition for "align to edge" [DrawingCenterMarkAlignToEdgeCtxCmd] but it doesn't seam to work in the loop.

 

I'm not sure this is possible with the API at this point in time.

Message 10 of 15

jwoodhouseD8XAE
Contributor
Contributor

I got it to work somewhat. This code creates a line from the center of the view to the center of the centermark, then runs the Align To Edge command. The user still has to select the line, but after each loop the line is deleted so there's only one option for the user to select. I've tried getting this to work where it automatically selects the line by using:

oAlignCmd.Execute2(False)

Then I've tried using oSelectSet.Add(oLine) and ThisApplication.CommandManager.DoSelect(oLine) and both of those will select the line, but not where the command will recognize the selection. Any suggestions here?

 

Here's the full code:

 

' Get the active drawing document
Dim oDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim oSheet As Sheet = oDoc.ActiveSheet
Dim oSelectSet As SelectSet = oDoc.SelectSet
oSelectSet.Clear

oSketch = oSheet.Sketches.Add

' Loop through each view on the sheet
For Each oView As DrawingView In oSheet.DrawingViews
    ' Get the center of the view
    Dim oViewCenter As Point2d = oView.Center
	Dim oViewMin As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oViewCenter.X - oView.Width/2, oViewCenter.Y - oView.Height/2)
    Dim oViewMax As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oViewCenter.X + oView.Width / 2, oViewCenter.Y + oView.Height / 2)
	'oSketch = oView.Sketches.Add
    ' Loop through all centermarks in the sheet
    For Each oMark As Centermark In oSheet.Centermarks
        Dim oMarkPos As Point2d = oMark.Position
		'make sure centermark is associated with current view
		If (oMarkPos.X >= oViewMin.X And oMarkPos.X <= oViewMax.X) And (oMarkPos.Y >= oViewMin.Y And oMarkPos.Y <= oViewMax.Y) Then			
			'make sure centermark isn't already aligned with the center by default
			If (oMarkPos.X <> oViewCenter.X And oMarkPos.X <> oViewCenter.X)
				oSketch.Edit
				oLine = oSketch.SketchLines.AddByTwoPoints(oViewCenter, oMarkPos)
				oSketch.ExitEdit
				oDoc.Update()

				oAlignCmd = ThisApplication.CommandManager.ControlDefinitions.Item("DrawingCenterMarkAlignToEdgeCtxCmd")
				oSelectSet.Select(oMark)
				oAlignCmd.Execute2(True) 'use true if using user input to select all the lines
				oSketch.Edit
				oLine.Delete
				oSketch.ExitEdit
			End If
		End If
    Next
Next
oSketch.Delete()
oInteraction = Nothing
oDoc.Update()

 

0 Likes
Message 11 of 15

WCrihfield
Mentor
Mentor

Hi @jwoodhouseD8XAE.  One of the often unwanted aspects about executing ControlDefinitions by code is that they are generally designed for user interface interactions, so they sometimes require manual user interaction to work, instead of code based manipulation.  And sometimes they will 'wait' for that user interaction to happen, and other times they may not.  Some will work with pre-selection, and some post-selection (most with both).  In this case, it seems like the command is waiting for manual user interaction, so it will not read the next line of code in your rule after the command is executed until that required manual user interaction happens, then it will resume to the next line of code.  Also, you generally do not need to use the Sketch.Edit() method or the Sketch.ExitEdit() method when doing things to sketches by code.  Those are primarily just for changing the 'edit mode' of the user interface, in preparation for manual user interactions.  You can usually create new SketchLines and delete them by code without being in the sketch's edit mode.

 

Also, just a suggestion but it may help improve efficiency and reduce processing by getting a few things to variables before any of the iterations start, such as the TransientGeometry object, the SketchLines collection object, and the ControlDefinition, then just use those variables within the iterations.  Won't fix the main issue though.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 12 of 15

jwoodhouseD8XAE
Contributor
Contributor

@WCrihfield Is there a way to programmatically move the cursor to a point on the line and click? I didn't see anything online about this being possible and I don't know if this would even count as a manual interaction, but I haven't been using iLogic very long so who knows? I know I can use False instead of True inside of Execute2() to keep running the code while the execute method is waiting for the input.

0 Likes
Message 13 of 15

WCrihfield
Mentor
Mentor

Yes...sort of, but likely not the way you need here.  The True vs False difference between those two Execute methods, controls synchronous vs asynchronous behavior, which is difficult to explain, but sort of means: will it interrupt any/all other processes/actions/commands that have already been scheduled in the que to be processed, or will it schedule this action at the end of the que to be processed, so that it waits its turn.  We can start/initiate a user interaction session with CommandManager.CreateInteractionEvents and the InteractionEvents.Start method and InteractionEvents.Stop method, which will allow user interactions, which it will then 'listen/monitor' for, so that we can react to those actions by code when they happen.  It also has the InteractionEvents.MouseEvents and InteractionEvents.SelectEvents, but that is getting into some relatively advanced areas of coding that would massively increase the size and complexity of the code.  Plus, since this command already initiates its own user interaction session, we don't have control over it from a code point of view, as far as I know.  I would likely have to do some local testing though to be absolutely sure about any possible alternatives.  I've just got a lot going on at work lately though, so I might not be able to get to it right away.

The CommandManager.PostPrivateEvent method came to mind briefly for this type of thing, but it does not offer a way to specify a SketchLine, so dead end there also.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 14 of 15

WCrihfield
Mentor
Mentor

I briefly searched in the Inventor Ideas area to see if anyone has requested this addition to the Inventor API yet, and only found requests for the manual abilities, which have mostly already been implemented.  So, you could make a new 'Idea' requesting that this ability be added into the Inventor API, like maybe a method of the Centermark object.  It's the long way around this issue, but who knows, maybe it will get a lot of attention and get implemented before too long.

Related ideas:

https://forums.autodesk.com/t5/inventor-ideas/center-mark-multi-select-align-with-edge/idi-p/5016094 

https://forums.autodesk.com/t5/inventor-ideas/allow-center-points-align-to-edge-command-to-pick-work... 

https://forums.autodesk.com/t5/inventor-ideas/edge-selection-for-aligning-drawing-view-dimensions/id...  

PS.  If you do, then you can post a link to this discussion within that idea, and also post a link to that Idea here in this discussion, so others can find it easier, and vote for it.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 15 of 15

jwoodhouseD8XAE
Contributor
Contributor

I've posted to the idea forum about the angle being a property of the centermark. 

 

This is the link to the post: Centermark Properties - Autodesk Community