VBA - How do I find the drawing view to which a centermark is attached?

VBA - How do I find the drawing view to which a centermark is attached?

gerrardhickson
Collaborator Collaborator
580 Views
6 Replies
Message 1 of 7

VBA - How do I find the drawing view to which a centermark is attached?

gerrardhickson
Collaborator
Collaborator

Hi All,

 

I have a drawing sheet with several views of the same part. More than one of those views contains centermarks.

I want to determine which drawing view each center mark belongs to. Additionally, some of these views are flat pattern views. 

From what I can tell, centermarks belong to the sheet object (not the drawing view), and they're attached to model geometry (not the drawing view). I can find out which model they're attached to, but I can't see which view they're attached to.

Some pseudo code to help explain:

Dim oSH As Sheet
Dim oCM As Centermark
Dim oDV As DrawingView

Set oSH = ThisApplication.ActiveDocument.ActiveSheet
For Each oDV In oSH.DrawingViews
    For Each oCM In oSH.Centermarks
        If oCM.<is attached to oDV> then
            'Do Stuff
        End If
    Next oCM
Next oDV
0 Likes
581 Views
6 Replies
Replies (6)
Message 2 of 7

A.Acheson
Mentor
Mentor

Maybe a little indirect but how about getting the position of the center mark then check if it is within the boundary box of the view. 

 

Another method would be to select the occurrence if you can get to that then loop through the browser nodes and detect the select browser node. The browser node path will contain the view name. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 7

gerrardhickson
Collaborator
Collaborator

As a workaround, I've taken the first approach. It's clumsy though. The second workaround crossed my mind, but I

went with the first for no particular reason.

 

For what its worth - there's no bounding box (or range box) for drawing views - you need to calculate the position +/- half the height/width.


Keen for a proper solution though.

0 Likes
Message 4 of 7

A.Acheson
Mentor
Mentor

I see, browser nodes is likely the only way I would think. Here is a helpful start if you not familiar. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 5 of 7

WCrihfield
Mentor
Mentor

Hi guys.  I picked this little routine up from another case I worked on a while back where a user wanted to identify which drawing view each of the drawing dimensions on the sheet belonged to.  There were a few ways to solve that task, but this is likely the most intuitive way, because it actually looks for the 'link' between the centermark and the view.  It's just a quick exploratory code right now that simply points out what each one is attached to, and how to get to the 'view' from there, and shows a message with the name of the view, but you get the point.  Something more refined could certainly be crafted if needed.

Dim oDDoc As DrawingDocument = ThisDoc.Document
Dim oSheet As Sheet = oDDoc.ActiveSheet
Dim oCMs As Centermarks = oSheet.Centermarks
For Each oCM As Centermark In oCMs
	If oCM.Attached = False Then Continue For
	oAttEnt = oCM.AttachedEntity
	MsgBox("TypeName(oAttEnt) = " & TypeName(oAttEnt), , "")
	If TypeOf oAttEnt Is GeometryIntent Then
		Dim oGI As GeometryIntent = oAttEnt
		If oGI.Geometry IsNot Nothing AndAlso _
			TypeOf oGI.Geometry Is DrawingCurve Then
			Dim oDC As DrawingCurve = oGI.Geometry
			Dim oDView As DrawingView = oDC.Parent
			MsgBox("oDView.Name = " & oDView.Name, , "")
		End If
	ElseIf TypeOf oAttEnt Is DrawingCurve Then
		Dim oDC As DrawingCurve = oAttEnt
		Dim oDView As DrawingView = oDC.Parent
		MsgBox("oDView.Name = " & oDView.Name,,"")
	End If
Next

Another process that was used to figure out which dimensions belonged to which views, was more of a process of elimination, where the view suppression was used to figure out which dimensions either remained, or were eliminated when that happened, then suppression was reversed.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 7

gerrardhickson
Collaborator
Collaborator

Well, I've come across this problem again on a different project. Initially, I used something like:

 

if oCM.AttachedEntity.Geometry.Parent is oDrawingView then
     'Do Stuff
endif

 

But I have found that this doesn't work if the centermark is a sheet metal punch. In this case, the AttachedEntity is a "FlatPunchResult" object, the parent of that is a 'FlatPattern', but I can't see any link between the sheet metal punch center mark and the drawing view.

 

Any suggestions?

 

EDIT: I just noticed @WCrihfield's mention of view suppression. I guess one solution is to suppress all views that I'm not interested in, then any remaining centermark must belong to the remaining drawing view. Hopefully, we can come up with a more direct option, but I'll accept this as a work around.

0 Likes
Message 7 of 7

WCrihfield
Mentor
Mentor

Hi @gerrardhickson.  As you have noticed, there are several possible 'types' of Centermarks (Link1, Link2), making the task of figuring out which view it is associated with a much more complicated that it seems like it should be.  I now have a custom function for this task, which combines the method shown in my code above, with the idea of the Centermark's position on the sheet being within the bounds of a view.  That position tactic doesn't work well with dimensions, because they can be positioned outside of the bounds of a view, but Centermarks can not be (as far as I know).  However, there is still the possibility of 2 or more views having overlapping boundaries, so this technique is still not 100% accurate.  I very rarely have view boundaries overlapping each other though, so it's not really a big issue for me.  Below is an example of its use:

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then Exit Sub
	Dim oDDoc As DrawingDocument = ThisDoc.Document
	Dim oSheet As Inventor.Sheet = oDDoc.ActiveSheet
	Dim oCMs As Centermarks = oSheet.Centermarks
	If oCMs.Count = 0 Then Exit Sub
	Dim oDict As New Dictionary(Of Inventor.DrawingView, List(Of Inventor.Centermark))
	For Each oCM As Centermark In oCMs
		Dim oView As DrawingView = GetCentermarkParentView(oCM)
		If oView Is Nothing Then Continue For
		If oDict.Keys.Contains(oView) Then
			oDict.Item(oView).Add(oCM)
		Else
			oDict.Add(oView, New List(Of Inventor.Centermark) From {oCM})
		End If
	Next 'oCM
	If oDict.Count > 0 Then
		For Each oEntry In oDict
			MsgBox("View Name = " & oEntry.Key.Name & vbCrLf & _
			"Centermarks Count = " & oEntry.Value.Count)
		Next 'oEntry
	End If	
End Sub

Function GetCentermarkParentView(oCM As Centermark) As DrawingView
	If oCM.Attached Then
		Dim oEnt As Object = oCM.AttachedEntity
		If TypeOf oEnt Is GeometryIntent Then
			Dim oGI As GeometryIntent = oEnt
			If oGI.Geometry IsNot Nothing AndAlso _
				TypeOf oGI.Geometry Is DrawingCurve Then
				Dim oDC As DrawingCurve = oGI.Geometry
				Return oDC.Parent
			End If
		ElseIf TypeOf oEnt Is DrawingCurve Then
			Dim oDC As DrawingCurve = oEnt
			Return oDC.Parent
		End If
	End If
	Dim oSheet As Inventor.Sheet = oCM.Parent
	Dim oViews As DrawingViews = oSheet.DrawingViews
	Dim oP2D As Point2d = oCM.Position
	Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
	For Each oView As DrawingView In oViews
		Dim oBox2D As Box2d = oTG.CreateBox2d
		oBox2D.MinPoint = oTG.CreatePoint2d(oView.Left, (oView.Top - oView.Height))
		oBox2D.MaxPoint = oTG.CreatePoint2d((oView.Left + oView.Width), oView.Top)
		If oBox2D.Contains(oP2D) Then Return oView
	Next
	Return Nothing
End Function

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes