Repositioning Detail View

Repositioning Detail View

dustinbagley
Advocate Advocate
390 Views
4 Replies
Message 1 of 5

Repositioning Detail View

dustinbagley
Advocate
Advocate

I've put together a rule that can reposition a detail view on to a work point. This rule was working the other day but now its saying that the 'object reference is not set to an instance of an object'

dustinbagley_0-1722440018259.png

For reference, the rule is below.

I'm not sure what the problem is because when I read the type of the object "oDetailView.Type.ToString" I get what I would expect, "kDetailDrawingViewObject", but when I try to write to "oDetailView.FenceCornerTwo.X" for example, it says this object is not set. I'm confused because it seems like it would be the same object and because its a detail drawing view object, it should have fence corners, (it is rectangular). It also was working recently. Any thoughts on what I'm missing? thanks, Dustin

dustinbagley_1-1722440318257.png

 

'///////////////////////////////////////////////////////////////////////////////////////////////
'  ---Create or Position Detail View---
'-----------------------------------------------------------------------------------------------
'  Description:
'  This rule will create or position a detail view at a named work point at the top level of a
'  model. Supported view orientation:
'  Top, Bottom, Front, Back, Right, Left, Bottom 180, Back 180, Right 90 and Left 270
'-----------------------------------------------------------------------------------------------
'  Calling method example:
'[Create or Position Detail View
'SharedVariable("CreatePosition") = "Create" 'Create/Position Create new view/position existin view
'SharedVariable("BaseViewName") = "TOP" ' "View Name" 
'SharedVariable("WorkPointName") = "Work Point 1" ' "Work Point Name"
'SharedVariable("DetailViewName") = "A" ' "Detail View Name"
'The following shared viariable are only required for the "Create" option
'SharedVariable("DetailViewWScale") = .375
'SharedVariable("DetailViewVerticalPos") = 50 'Percentatge across the sheet from left
'SharedVariable("DetailViewHorizontalPos") = 50 'Percentatge across the sheet from bottom
'SharedVariable("FenceSize") = 2 'Numeric Value, Rad. or Rect. Height
'SharedVariable("FenceWidth") = 2 'Numeric Value, Rect. Width
'SharedVariable("FenceShape") = "Circ" ' "Rect"/"Circ" 
'SharedVariable("Debug") = False
'iLogicVb.RunExternalRule("Create or Position Detail View")
']
'-----------------------------------------------------------------------------------------------

'[ Rule explanation and iLogic tracking log start entry
'This rule handles the values that are sent down to the sub-components of the assembly
'and getting text parameter values from a sub-component.

'SharedVariable("RuleName") = "Drawing Parameter Control"
'SharedVariable("RulePrefix") = "Start"
'iLogicVb.RunExternalRule("Application iLogic Tracking Log Entry")
']

Dim oDrawDoc As DrawingDocument = ThisApplication.ActiveDocument
Dim oSheet, oSheetTemp As Sheet
Dim oViewLooper, oViewTemp, oView As DrawingView
Dim oAssyDoc As AssemblyDocument
Dim oWP, oSelectedWP As WorkPoint
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oPointBase, oPointCorner1, oPointCorner2, oViewOrigin, oViewWP As Point2d
Dim MinQuandrant As Integer = 0
Dim RotateCounter As Integer = 0
Dim BaseViewOrientation As String
Dim Undo As Transaction
Dim X2D As Double
Dim Y2D As Double
Dim X3D As Double
Dim Y3D As Double
Dim Z3D As Double
Dim CreatePosition As String
Dim BaseViewName As String
Dim WorkPointName As String
Dim DetailViewName As String
Dim DetailViewWScale As String
Dim DetailViewVerticalPos As Integer
Dim DetailViewHorizontalPos As Integer
Dim FenceSize As Double
Dim FenceWidth As Double
Dim FenceShape As String
Dim FenceSizeSet As Boolean
Dim Debug As Boolean

'[Checks document type
If Not ThisDoc.Document.DocumentType = kDrawingDocumentObject Then
	MessageBox.Show("'Create or Position Detail View' only works in a drawing.", _
	"Create or Position Detail View", MessageBoxButtons.OK, MessageBoxIcon.Error)
	GoTo 0 'Exits Rule
End If
']

'[Check for shared variables
If SharedVariable.Exists("CreatePosition") Then
	CreatePosition = SharedVariable("CreatePosition")
	SharedVariable.Remove("CreatePosition")
	If CreatePosition = "Create"
	elseIf CreatePosition =	"Position"
	Else
		MessageBox.Show("'CreatePosition' Variable on accepts the inputs: Create or Position.", _
		"Create or Position Detail View", MessageBoxButtons.OK, MessageBoxIcon.Error)
		GoTo 0
	End If
Else
	CreatePosition = "Create"
End If

If SharedVariable.Exists("BaseViewName") Then
	BaseViewName = SharedVariable("BaseViewName")
	SharedVariable.Remove("BaseViewName")
Else
	BaseViewName = "TOP"
End If

If SharedVariable.Exists("WorkPointName") Then
	WorkPointName = SharedVariable("WorkPointName")
	SharedVariable.Remove("WorkPointName")
Else
	WorkPointName = "Work Point1"
End If

If SharedVariable.Exists("DetailViewName") Then
	DetailViewName = SharedVariable("DetailViewName")
	SharedVariable.Remove("DetailViewName")
Else
	DetailViewName = "A"
End If

If SharedVariable.Exists("DetailViewWScale") Then
	DetailViewWScale = SharedVariable("DetailViewWScale")
	SharedVariable.Remove("DetailViewWScale")
Else
	DetailViewWScale = .375
End If

If SharedVariable.Exists("DetailViewVerticalPos") Then
	DetailViewVerticalPos = SharedVariable("DetailViewVerticalPos")
	SharedVariable.Remove("DetailViewVerticalPos")
Else
	DetailViewVerticalPos = 50
End If

If SharedVariable.Exists("DetailViewHorizontalPos") Then
	DetailViewHorizontalPos = SharedVariable("DetailViewHorizontalPos")
	SharedVariable.Remove("DetailViewHorizontalPos")
Else
	DetailViewHorizontalPos = 50
End If

If SharedVariable.Exists("FenceSize") Then
	FenceSize = SharedVariable("FenceSize")
	SharedVariable.Remove("FenceSize")
	FenceSizeSet = True
Else
	FenceSize = 2
	FenceSizeSet = False
End If

If SharedVariable.Exists("FenceWidth") Then
	FenceWidth = SharedVariable("FenceWidth")
	SharedVariable.Remove("FenceWidth")
Else
	FenceWidth = 2
End If

If SharedVariable.Exists("FenceShape") Then
	FenceShape = SharedVariable("FenceShape")
	SharedVariable.Remove("FenceShape")
	If FenceShape = "Rect"
	ElseIf FenceShape = "Circ"
	Else
		MessageBox.Show("'FenceShape' Variable on accepts the inputs: Rect or Circ.", _
		"Create or Position Detail View", MessageBoxButtons.OK, MessageBoxIcon.Error)
		GoTo 0
	End If
Else
	FenceShape = "Rect"
End If

If SharedVariable.Exists("Debug") Then
	Debug = SharedVariable("Debug")
	SharedVariable.Remove("Debug")
Else
	Debug = False
End If
']

Undo = ThisApplication.TransactionManager.StartTransaction(oDrawDoc, "Create or Position Detail View")

'[Checks if named base view exists or if it is suppressed
For Each oSheet In oDrawDoc.Sheets
	For Each oViewLooper In oSheet.DrawingViews
		If oViewLooper.Name = BaseViewName Then
			If oViewLooper.Suppressed = True Then
				GoTo 0 'Exits Rule
			End If
			oView = oViewLooper
		End If
	Next
Next
If oView Is Nothing Then
	MessageBox.Show("Cannot find base view named: " & BaseViewName, _
	"Create or Position Detail View", MessageBoxButtons.OK, MessageBoxIcon.Error)
	GoTo 0 'Exits Rule
End If
']

'[Sets sheet number And assembly document
oSheet = oView.Parent
oAssyDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
']

'[Gets work point coordinates
For Each oWP In oAssyDoc.ComponentDefinition.WorkPoints
	If oWP.Name = WorkPointName Then
		oSelectedWP = oWP
	End If
Next
If oSelectedWP Is Nothing
	MessageBox.Show("Cannot find work point named: " & WorkPointName, _
	"Create or Position Detail View", MessageBoxButtons.OK, MessageBoxIcon.Error)
	GoTo 0 'Exits Rule
End If
']

'[Gets Min and Max points for range box (opposite extents of visible assembly)
oRangeBoxMin = oAssyDoc.ComponentDefinition.RangeBox.MinPoint
oRangeBoxMax = oAssyDoc.ComponentDefinition.RangeBox.MaxPoint
']

'[Gets Lengths of range box
XRangeBox = oRangeBoxMax.X - oRangeBoxMin.X
YRangeBox = oRangeBoxMax.Y - oRangeBoxMin.Y
ZRangeBox = oRangeBoxMax.Z - oRangeBoxMin.Z
']

'[Rereferenced work point to range box and scales them to drawing view.
X3D = (oSelectedWP.Point.X - oRangeBoxMin.X) * oView.Scale
Y3D = (oSelectedWP.Point.Y - oRangeBoxMin.Y) * oView.Scale
Z3D = (oSelectedWP.Point.Z - oRangeBoxMin.Z) * oView.Scale
']

'[Determines the orientation  and roation of the base view.
If oView.Camera.ViewOrientationType.ToString = "kArbitraryViewOrientation"
	oSheetTemp = oDrawDoc.Sheets.Add
	oViewTemp = oView.CopyTo(oSheetTemp)
	1:
	If oViewTemp.Camera.ViewOrientationType.ToString <> "kArbitraryViewOrientation"
		BaseViewOrientation = oViewTemp.Camera.ViewOrientationType.ToString
		oViewTemp.Delete
		oSheetTemp.Delete
		oSheet.Activate
	ElseIf oViewTemp.Camera.ViewOrientationType.ToString = "kArbitraryViewOrientation"
		'Sometime the rotate funtion fails, in which case an alternate method of rotating is used
		Try
			oViewTemp.RotateByAngle(90 * PI / 180)
		Catch
			oSS = oDrawDoc.SelectSet
			oSS.Clear
			oSS.Select(oViewTemp)
			oCD = ThisApplication.CommandManager.ControlDefinitions.Item("DrawingViewRotateCtxCmd")
			oSS.Select(oViewTemp)
			oCD.Execute2(False)
			AppActivate(ThisApplication.Caption)
			System.Windows.Forms.SendKeys.SendWait("{DOWN}^A90{TAB}{UP}{ENTER}")
		End Try
		RotateCounter = RotateCounter + 90
		If RotateCounter > 270 Then
			MessageBox.Show("Something went wrong. Results may not be as intended.", _
			"Create or Position Detail View", MessageBoxButtons.OK, MessageBoxIcon.Error)
			GoTo 2
		End If
		GoTo 1
	End If
ElseIf oView.Camera.ViewOrientationType.ToString = "kTopViewOrientation"
	BaseViewOrientation = oView.Camera.ViewOrientationType.ToString
	RotateCounter = 0
ElseIf oView.Camera.ViewOrientationType.ToString = "kBottomViewOrientation" 
	BaseViewOrientation = oView.Camera.ViewOrientationType.ToString
	RotateCounter = 0
ElseIf oView.Camera.ViewOrientationType.ToString = "kFrontViewOrientation"  
	BaseViewOrientation = oView.Camera.ViewOrientationType.ToString
	RotateCounter = 0
ElseIf oView.Camera.ViewOrientationType.ToString = "kBackViewOrientation"   
	BaseViewOrientation = oView.Camera.ViewOrientationType.ToString
	RotateCounter = 0
ElseIf oView.Camera.ViewOrientationType.ToString = "kRightViewOrientation"  
	BaseViewOrientation = oView.Camera.ViewOrientationType.ToString
	RotateCounter = 0
ElseIf oView.Camera.ViewOrientationType.ToString = "kLeftViewOrientation"   
	BaseViewOrientation = oView.Camera.ViewOrientationType.ToString
	RotateCounter = 0
End If
2:
']
'RotateCounter, MinQuadrant
'[Sets quadrant of min. range box and translates the model axis to the drawing view axis.
If BaseViewOrientation = "kTopViewOrientation" And RotateCounter = 0 Then
	MinQuadrant = 2
	X2D = X3D
	Y2D = -Z3D
ElseIf BaseViewOrientation = "kBottomViewOrientation" And RotateCounter = 0 Then
	MinQuadrant = 3
	X2D = X3D
	Y2D = Z3D
ElseIf BaseViewOrientation = "kBottomViewOrientation" And RotateCounter = 180 Then
	MinQuadrant = 1
	X2D = -X3D
	Y2D = -Z3D
ElseIf BaseViewOrientation = "kFrontViewOrientation" And RotateCounter = 0 Then
	MinQuadrant = 3
	X2D = X3D
	Y2D = Y3D
ElseIf BaseViewOrientation = "kBackViewOrientation" And RotateCounter = 0 Then
	MinQuadrant = 4
	X2D = -X3D
	Y2D = Y3D
ElseIf BaseViewOrientation = "kBackViewOrientation" And RotateCounter = 180 Then 
	MinQuadrant = 2
	X2D = X3D
	Y2D = -Y3D
ElseIf BaseViewOrientation = "kRightViewOrientation" And RotateCounter = 0 Then
	MinQuadrant = 4
	X2D = -Z3D
	Y2D = Y3D
ElseIf BaseViewOrientation = "kRightViewOrientation" And RotateCounter = 90 Then
	MinQuadrant = 1
	X2D = -Y3D
	Y2D = -Z3D
ElseIf BaseViewOrientation = "kLeftViewOrientation" And RotateCounter = 0 Then
	MinQuadrant = 3
	X2D = Z3D
	Y2D = Y3D
ElseIf BaseViewOrientation = "kLeftViewOrientation" And RotateCounter = 270 Then 
	MinQuadrant = 2
	X2D = Y3D
	Y2D = -Z3D
ElseIf BaseViewOrientation = "kFrontViewOrientation" And RotateCounter = 90 Then 
	MinQuadrant = 4
	X2D = -Y3D
	Y2D = X3D	
ElseIf BaseViewOrientation = "kFrontViewOrientation" And RotateCounter = 270 Then 
	MinQuadrant = 2
	X2D = Y3D
	Y2D = -X3D
Else
	MessageBox.Show("View orientation: " & BaseViewOrientation & " and roation: " & RotateCounter & _
	" combination are not supported in this rule. Please add the combination to the rule." & _
	" Refer to 'Create or Position Detail Rule 3D to 2D Calculator' in the iLogic Folder", _
	"Create or Position Detail View", MessageBoxButtons.OK, MessageBoxIcon.Error)
	GoTo 0 'Exits Rule
End If
']

'[Creates 2D point at drawing view corner associated with range box min.
If MinQuadrant = 1 Then
	oViewOrigin = oTG.CreatePoint2d((oView.Position.X + oView.Width / 2), (oView.Position.Y + oView.Height / 2))
ElseIf MinQuadrant = 2 Then
	oViewOrigin = oTG.CreatePoint2d((oView.Position.X - oView.Width / 2), (oView.Position.Y + oView.Height / 2))
ElseIf MinQuadrant = 3 Then
	oViewOrigin = oTG.CreatePoint2d((oView.Position.X - oView.Width / 2), (oView.Position.Y - oView.Height / 2))
ElseIf MinQuadrant = 4 Then
	oViewOrigin = oTG.CreatePoint2d((oView.Position.X + oView.Width / 2), (oView.Position.Y - oView.Height / 2))
End If
']

'[Checks for inconsistancies between rangebox and drawing detail view.
If Debug = True Then
	If oView.Width = XRangeBox * oView.Scale Or oView.Width = YRangeBox * oView.Scale Or oView.Width = ZRangeBox * oView.Scale And _
		oView.Height = XRangeBox * oView.Scale Or oView.Width = YRangeBox * oView.Scale Or oView.Width = ZRangeBox * oView.Scale Then
		'MsgBox("Range box equals drawing view")
	Else
		MessageBox.Show("Range box does not equal drawing view. The detail fence will not be positioned correctly. This may be a result of " & _
		"additional solids visible in the model that are not visible in the drawing; or the range box information is out of date.", _
		"Create or Position Detail View", MessageBoxButtons.OK, MessageBoxIcon.Error)
		'GoTo 0 'Exits Rule
	End If
Else
End If
']

'[Creates 2D point at drawing view associated with work point.
oViewWP = oTG.CreatePoint2d((oViewOrigin.X + X2D), (oViewOrigin.Y + Y2D))
']

'[Create or position detail view.
Dim oDetailView As DetailDrawingView
Dim DetailViewVerticalPerc As Double
Dim DetailViewHorizontalPerc As Double

'Converts user percentage inputs to linear sheet distance
DetailViewHorizontalPerc = DetailViewHorizontalPos/100 * oSheet.Width
DetailViewVerticalPerc = DetailViewVerticalPos/100 * oSheet.Height

oPointBase = oTG.CreatePoint2d(DetailViewHorizontalPerc, DetailViewVerticalPerc)
oPointCorner1 = oTG.CreatePoint2d(oViewWP.X - (FenceWidth / 2), oViewWP.Y - (FenceSize / 2))
oPointCorner2 = oTG.CreatePoint2d(oViewWP.X + (FenceWidth / 2), oViewWP.Y + (FenceSize / 2))
	
'Creates rectangular or circular detail view
If CreatePosition = "Create" Then		
	If FenceShape = "Rect" Then
		oDetailView = oSheet.DrawingViews.AddDetailView(oView, oPointBase, kFromBaseDrawingViewStyle, False, _ 
			oPointCorner1, oPointCorner2, , DetailViewWScale, True, DetailViewName, )
	ElseIf FenceShape = "Circ" Then
		oDetailView = oSheet.DrawingViews.AddDetailView(oView, oPointBase, kFromBaseDrawingViewStyle, True, _ 
			oViewWP, FenceSize/2, , DetailViewWScale, True, DetailViewName, )	
	End If
	
'Positions existing detail view
ElseIf CreatePosition = "Position" Then
	For Each oSheet In oDrawDoc.Sheets
		For Each oViewLooper In oSheet.DrawingViews
			If oViewLooper.Name = DetailViewName Then
				oDetailView = oViewLooper
			End If
		Next
	Next
	If oDetailView Is Nothing Then
		MessageBox.Show("Cannot find detail view named: " & DetailViewName, _
		"Create or Position Detail View", MessageBoxButtons.OK, MessageBoxIcon.Error)
		GoTo 0 'Exits Rule
	End If
	If FenceShape = "Rect" Then
		If FenceSizeSet = False
			'Gets current size of rect. fence.
			msgbox(oDetailView.Type.ToString)
			FenceWidth = Abs(oDetailView.FenceCornerTwo.X)-Abs(oDetailView.FenceCornerOne.X)
			FenceSize = Abs(oDetailView.FenceCornerTwo.Y) -Abs(oDetailView.FenceCornerOne.Y)
		End If
	
		'Attached view warning
		Try
			If oDetailView.AttachPoint.Geometry Is Nothing Then
				
			Else
				oDetailView.AttachPoint = Nothing
				'MsgBox("Attached")
				MsgBox(oDetailView.AttachPoint.Geometry.ToString)
					MessageBox.Show("View: " & oDetailView.Name & " should not be attached. Please detach it and try again.", _
					"Create or Position Detail View", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End If	
		Catch
			'MsgBox("Not Attached")
		End Try	
		
		'Relocates point to current size of rect. fence.
		oPointCorner1 = oTG.CreatePoint2d(oViewWP.X - (FenceWidth / 2), oViewWP.Y - (FenceSize / 2))
		oPointCorner2 = oTG.CreatePoint2d(oViewWP.X + (FenceWidth / 2), oViewWP.Y + (FenceSize / 2))
		'Repositions corners of rect. fence.

			msgbox(oDetailView.AttachPoint.ToString)
			oDetailView.FenceCornerOne = oPointCorner1
			oDetailView.FenceCornerTwo = oPointCorner2

	ElseIf FenceShape = "Circ" Then
		oDetailView.FenceCenter = oViewWP
		If FenceSizeSet = True Then
			oDetailView.FenceRadius = FenceSize
		End If
	End If
End If
']

Undo.End

0 :

'[ iLogic tracking log finish entry
'SharedVariable("RuleName") = "Drawing Parameter Control"
'SharedVariable("RulePrefix") = "Finish"
'iLogicVb.RunExternalRule("Application iLogic Tracking Log Entry")
']

 

0 Likes
Accepted solutions (1)
391 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

Hi @dustinbagley.  Your code example was pretty long, so I did not fully review every line of it, but just based on your statements, I think I know one of the things that is going on.  The DetailDrawingView.FenceCornerTwo says that it is Read/Write, and that its Value Type is a Point2d.  This means, if you want to change its value, you can not simply modify its current value directly, but you should supply a different Point2d object as its value.  If you need to, you can use its Copy method to obtain a copy of its current value to a Point2d Type variable, then modify that instance through that variable, then supply that variable instance back to that property, as its new value.  This is mostly because Point2d is a 'transient' object (mathematical data in an organized package, designed for transferring that data in a meaningful way).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 5

dustinbagley
Advocate
Advocate

Thanks for your response. In this rule, I believe I am trying to write to both DetailDrawingView.FenceCornerTwo.X and DetailDrawingView.FenceCornerTwo.Y individually with doubles. Do you still see that as possibly causing the issue?

0 Likes
Message 4 of 5

WCrihfield
Mentor
Mentor
Accepted solution

After copying your code into Notepad, then checking which line is Line 407 (mentioned in the error message), I see that that line of code:

FenceWidth = Abs(oDetailView.FenceCornerTwo.X)-Abs(oDetailView.FenceCornerOne.X)

...is only attempting to 'read' those values, not 'write/set' them, so my initial assumption may be wrong.  Plus, several lines of code below that position, I see that you are already creating new Point2d objects, and setting those as the values of those two properties, which is the correct way to do it.  So, since I only see one 'object' variable being used in that line of code that I think could possibly cause that type of error (oDetailView), and you have already checked if that was 'Nothing' a few lines earlier...my new assumption is that the 'fence' must either be 'circular', or there may just be something else wrong with the 'fence'.  Because the online help documentation says that the FenceCornerOne & FenceCornerTwo properties may be 'Nothing', if the fence is circular.  That would cause that type of error.  I see that you seem to be attempting to check fence shape before this point, but maybe something about that check needs to be reviewed or updated.  And maybe add in a check to see of those two properties return Nothing, before attempting to do math with their values of their properties.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 5

dustinbagley
Advocate
Advocate

You were right about reviewing the way I was checking the fence shape. I've used the following to actually test the fence shape:

dustinbagley_1-1722460185204.png

 

The weird thing though is that it comes back and says that the fence shape is circular. You can see below that it is clearly rectangular. 

dustinbagley_0-1722459717819.png

 

I only have one detail view showing in the browser. So I ran another rule that counted the number of detail views in the drawing and discovered that there are 3 with the same name but only one of them is rectangular. 

dustinbagley_2-1722461359735.png

I don't know why I have these other views but I think I know what I need to do now (run a better check for fence shapes and possibly views with duplicate names). Thanks for your help and steering me in the right direction, 

 

Dustin 



 

0 Likes