Hiding orphaned centerlines and centermarks

Hiding orphaned centerlines and centermarks

ngowans
Contributor Contributor
606 Views
5 Replies
Message 1 of 6

Hiding orphaned centerlines and centermarks

ngowans
Contributor
Contributor

I am trying to make a simple script which will go through the sheets of my drawing file, find all of the centerline or centermark annotations (eventually I will add dimensions too) and will check them for being attached, if they are not attached then they will be put on an Orphan dims layer which isn't printed.

 

The code I have below seems to work for centermarks but doesn't seem to work for centerlines for some reason even though the subs are apparently very similar.  

 

My next step will be to add Else statements to the subroutines in this script to force the annotation back to it's original state in the event that it becomes reattached. However in order to do that, I need to know the Users style settings to record their "Default" layer for said annotation. How do I retrieve that?

 

I'm very new to scripting so if anyone can give me any advice in general then that would be massively appreciated. 

 

Sub Main()
 
oHideOption = InputRadioBox("Select an option:", _
"Hide Orphan Dimensions", "Show Orphan Dimensions", True, "iLogic")
Dim oDoc As Document = ThisDoc.Document


Try 
oOrphanLayer = oDoc.StylesManager.Layers.Item(1).Copy("Orphan Dims")   
Catch
oOrphanLayer = oDoc.StylesManager.Layers.Item("Orphan Dims")  
End Try

	If oHideOption = True Then
			For Each oSheet In oDoc.Sheets
				HideCenterlines(oSheet, oOrphanLayer)
				HideCentermarks(oSheet, oOrphanLayer)
			Next
	Else
	 
	oOrphanLayer.Visible = True
	End If
	
oDoc.Update 

End Sub

Sub HideCenterlines(oSheet, oOrphanLayer)
		Try
			Dim oDrawingCTM As Centerline
			For Each oDrawingCTL In oSheet.Centerlines
			If oDrawingCTL.Attached = False Then
			oDrawingCTL.Layer = oOrphanLayer
			End If
		
			Next
		Catch
			MessageBox.Show("Error in HCL")
		End Try
End Sub

Sub HideCentermarks(oSheet, oOrphanLayer)
		Try
			Dim oDrawingCTM As Centermark
			For Each oDrawingCTM In oSheet.Centermarks
			If oDrawingCTM.Attached = False Then
			oDrawingCTM.Layer = oOrphanLayer
			End If
		
			Next
		Catch
			MessageBox.Show("Error in HCM")
		End Try
End Sub

 

0 Likes
Accepted solutions (3)
607 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @ngowans.  Try this edited version of your rule.

Sub Main()
	 oHideOption = InputRadioBox("Select an option:", _
	"Hide Orphan Dimensions", "Show Orphan Dimensions", True, "iLogic")
	Dim oDDoc As DrawingDocument = ThisDrawing.Document
	Dim oOrphanLayer As Layer
	Try
		oOrphanLayer = oDDoc.StylesManager.Layers.Item("Orphan Dims")    
	Catch
		oOrphanLayer = oDDoc.StylesManager.Layers.Item(1).Copy("Orphan Dims")
	End Try
	If oHideOption = True Then
		For Each oSheet As Sheet In oDDoc.Sheets
			HideCenterlines(oSheet, oOrphanLayer)
			HideCentermarks(oSheet, oOrphanLayer)
		Next
	Else
		oOrphanLayer.Visible = True
	End If
	oDDoc.Update 
End Sub

Sub HideCenterlines(oSheet As Sheet, oOrphanLayer As Layer)
	For Each oDrawingCTL As Centerline In oSheet.Centerlines
		If oDrawingCTL.Attached = False Then
			Try
				oDrawingCTL.Layer = oOrphanLayer
			Catch oEx As Exception
				MessageBox.Show("Error in HCL" & vbCrLf & _
				oEx.Message & vbCrLf & oEx.StackTrace)
			End Try
		End If
	Next
End Sub

Sub HideCentermarks(oSheet As Sheet, oOrphanLayer As Layer)
	For Each oDrawingCTM As Centermark In oSheet.Centermarks
		If oDrawingCTM.Attached = False Then
			Try
				oDrawingCTM.Layer = oOrphanLayer
			Catch oEx As Exception
				MessageBox.Show("Error in HCM" & vbCrLf & _
				oEx.Message & vbCrLf & oEx.StackTrace)
			End Try
		End If
	Next
End Sub

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

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 6

WCrihfield
Mentor
Mentor
Accepted solution

As for knowing which layer they should be put back on when they are fixed (re-attached)...there is actually a way to get the 'default' layer for those types of things.  Here is an example:

	Dim oDDoc As DrawingDocument = ThisDrawing.Document
	oDefaultCLLayer = oDDoc.StylesManager.ActiveStandardStyle.ActiveObjectDefaults.CenterlineLayer
	oDefaultCMLayer = oDDoc.StylesManager.ActiveStandardStyle.ActiveObjectDefaults.CentermarkLayer

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

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 6

ngowans
Contributor
Contributor

This works but I'm trying to figure out where I went wrong, 

 

It seems to specified some of the variables, and changed the name of a couple of others and added some error handling.

 

Can you explain what was wrong with the original code so I can better understand where I failed? 

 

 

Sub Main()
	Dim oDDoc As DrawingDocument = ThisDrawing.Document
	Dim oOrphanLayer As Layer
	oDefaultCLLayer = oDDoc.StylesManager.ActiveStandardStyle.ActiveObjectDefaults.CenterlineLayer
	oDefaultCMLayer = oDDoc.StylesManager.ActiveStandardStyle.ActiveObjectDefaults.CentermarkLayer
	
	Try
		oOrphanLayer = oDDoc.StylesManager.Layers.Item("Orphan Dims")    
	Catch
		oOrphanLayer = oDDoc.StylesManager.Layers.Item(1).Copy("Orphan Dims")
	End Try
		
		
		For Each oSheet As Sheet In oDDoc.Sheets
			HideCenterlines(oSheet, oOrphanLayer, oDefaultCLLayer)
			HideCentermarks(oSheet, oOrphanLayer, oDefaultCMLayer)
		Next

	oDDoc.Update 
End Sub

Sub HideCenterlines(oSheet As Sheet, oOrphanLayer As Layer, oDefaultCLLayer As Layer)
	For Each oDrawingCTL As Centerline In oSheet.Centerlines
		If oDrawingCTL.Attached = False Then
			Try
				oDrawingCTL.Layer = oOrphanLayer
			Catch oEx As Exception
				MessageBox.Show("Error in HCL" & vbCrLf & _
				oEx.Message & vbCrLf & oEx.StackTrace)
			End Try
		Else If oDrawingCTL.Attached = True Then
			Try
				oDrawingCTL.Layer = oDefaultCLLayer
			Catch oEx As Exception
				MessageBox.Show("Error in HCM return" & vbCrLf & _
				oEx.Message & vbCrLf & oEx.StackTrace)
			End Try
		End If
	Next
End Sub

Sub HideCentermarks(oSheet As Sheet, oOrphanLayer As Layer, oDefaultCMLayer As Layer)
	For Each oDrawingCTM As Centermark In oSheet.Centermarks
		If oDrawingCTM.Attached = False Then
			Try
				oDrawingCTM.Layer = oOrphanLayer
			Catch oEx As Exception
				MessageBox.Show("Error in HCM" & vbCrLf & _
				oEx.Message & vbCrLf & oEx.StackTrace)
			End Try
		Else If oDrawingCTM.Attached = True Then
			Try
				oDrawingCTM.Layer = oDefaultCMLayer
			Catch oEx As Exception
				MessageBox.Show("Error in HCM return" & vbCrLf & _
				oEx.Message & vbCrLf & oEx.StackTrace)
			End Try
		End If
	Next
End Sub

 

I've added the final solution as above, this now removes the radio button and then automatically toggles any unattached annotations to the hidden layer and turns any annotation that is attached onto it's pre-set default layer. 

 

I just have to add Dimensions, Leaders and Bubbles to the same and it should save me lots of time. 

 

The last question is;

Rather than creating a separate sub to go through each annotation type, is it possible to define a variable as "any annotations" so that I can reduce the code to be more efficient. 

0 Likes
Message 5 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @ngowans.  Sure.  I will try to explain the edits I made, and why I made them.  As well as what we can do to make it even more efficient.

 

The first thing I did was change the line of code for getting the target document.  There are many ways to find/identify/get a document to target, but some are more appropriate/advantageous/efficient than others in certain situations (Link1).  If you know what type of document you are going to get to be working with, it is always better to define the document variable as that more specific type of document, rather than a general/generic document, because then you will have more help from the 'Intellisense' type system (pop-up help/hints) in the iLogic rule editor environment, and will have all of properties and methods specific to that document type naturally available to you.

 

The next thing I did was switch the order around within the Try...Catch block.  I did that because we first want to see if the Layer already exists then get it if it does, before we try to create another one.  This way it will only attempt to create a new layer, if it does not already exist or can't be found.  By the way, that Item("Orphan Dims") is looking for the layer's 'InternalName' which is most likely different than its regular Name.

 

Then in the definition line of both Sub routines, I added in the 'As Type' behind each input variable, so that the Sub know what Type of objects those variable represent.  Also, typically the input variables in a Sub or Function are not spelled the same as similar variables in the calling routine, because there is a disconnect there.  I could have additionally added the keywords ByVal or ByRef just before each input variable.  ByVal means that nothing you do with that variable will effect the source object within the calling routine.  If you use ByRef, then that is like sharing the same object directly from the source.  Also, in the first couple of lines of your HideCenterlines Sub, there was a spelling difference in the variable name.

Dim oDrawingCTM As Centerline
For Each oDrawingCTL In oSheet.Centerlines

So I simply got rid of that first line in both routines, and just included the Type designation in the first line of the loop.  Then I also narrowed down what code was included within the Try...Catch blocks, and included more info within the Catch side, simply because it is usually good practice to do so.

 

You could either just completely get rid of the two sub routines, and just incorporate most of their functionality within the Sub Main area, or you could combine the two into one larger one, since you are not actually passing the centerline or centermark objects to them specifically.  In a sense, you are already passing a generic enough object type to those routines, because the Sheet includes all the Centerlines, Centermarks, DrawingDimensions, & DrawingNotes (etc.). 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 6

ngowans
Contributor
Contributor

Thank you so much for taking the time to explain this, I think I have a long long way to go but everything I learn is making it easier to understand. 

0 Likes