If Statement for if a string is a number.

If Statement for if a string is a number.

C_Haines_ENG
Collaborator Collaborator
551 Views
8 Replies
Message 1 of 9

If Statement for if a string is a number.

C_Haines_ENG
Collaborator
Collaborator

I am looking to add a If or Case statement to some code. It updates the label name for all drawing views on a sheet but I would like it to only overwrite standard view names and numbers as that is what the view labels default to.

 

I cant seem to do it by converting the string to a double as actual text cant seem to be converted and if it converts all text to a number than the custom values I don't want it to change will be overwritten.

 

Does anyone have a solution for this? I've put the code I have below. 

 

'[ ASSEMBLY OR PART
oView = ActiveSheet.DrawingViews.NativeEntity.Item(1)
oRDD = oView.ReferencedDocumentDescriptor
Dim oModelType As String
If oRDD.ReferencedDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
	oModelType = "Assembly"
ElseIf oRDD.ReferencedDocumentType = DocumentTypeEnum.kPartDocumentObject Then
	oModelType = "Part"
End If
']

'[ VIEW SELECTOR
Dim tmpSheet As Sheet = ThisDrawing.ActiveSheet.Sheet
Dim tmpView As DrawingView
For Each tmpView In tmpSheet.DrawingViews
		Dim myDrawingViewName As String = ""
		If tmpView.Type = ObjectTypeEnum.kDrawingViewObject Then
			'Encapsulat the rotation of drawing view in a transaction
			Dim oTrans As Transaction
			oTrans = ThisApplication.TransactionManager.StartTransaction(ThisDrawing.Document, "Rotate Drawing View temp")
			
				' Set rotation to 0
			If tmpView.Camera.ViewOrientationType = ViewOrientationTypeEnum.kArbitraryViewOrientation Then
				If tmpView.Rotation<>0 Then
					tmpView.Rotation = 0
				End If
	    	End If
	
	Dim MyDrawingViewNameNUM As Double = tmpView.Name
']

'[ CASE SELECTOR
Select Case tmpView.Name
	
Case "BACK", "BOTTOM", "FRONT", "SIDE", "TOP", "O/S FACE", "I/S FACE", "END", MyDrawingViewNameNUM <2000
']

'[ PART VIEW LABELS
If oModelType = "Assembly"

    Select Case tmpView.Camera.ViewOrientationType
        Case ViewOrientationTypeEnum.kBackViewOrientation
            myDrawingViewName = "BACK"
        Case ViewOrientationTypeEnum.kBottomViewOrientation
            myDrawingViewName = "BOTTOM"
        Case ViewOrientationTypeEnum.kFrontViewOrientation
            myDrawingViewName = "FRONT"
        Case ViewOrientationTypeEnum.kIsoBottomLeftViewOrientation
            myDrawingViewName = "ISOMETRIC"
        Case ViewOrientationTypeEnum.kIsoBottomRightViewOrientation
            myDrawingViewName = "ISOMETRIC"
        Case ViewOrientationTypeEnum.kIsoTopLeftViewOrientation
            myDrawingViewName = "ISOMETRIC"
        Case ViewOrientationTypeEnum.kIsoTopRightViewOrientation
            myDrawingViewName = "ISOMETRIC"
        Case ViewOrientationTypeEnum.kLeftViewOrientation
            myDrawingViewName = "SIDE"
        Case ViewOrientationTypeEnum.kRightViewOrientation
            myDrawingViewName = "SIDE"
        Case ViewOrientationTypeEnum.kTopViewOrientation
            myDrawingViewName = "TOP"
        Case Else
            myDrawingViewName = ""
    End Select
    If Not myDrawingViewName = "" Then
        tmpView.Name = myDrawingViewName
    End If

    myDrawingViewName = ""

End If
']

'[ PART VIEW LABELS
If oModelType = "Part"

    Select Case tmpView.Camera.ViewOrientationType
        Case ViewOrientationTypeEnum.kBackViewOrientation
            myDrawingViewName = "O/S FACE"
        Case ViewOrientationTypeEnum.kBottomViewOrientation
            myDrawingViewName = "BOTTOM"
        Case ViewOrientationTypeEnum.kFrontViewOrientation
            myDrawingViewName = "I/S FACE"
        Case ViewOrientationTypeEnum.kIsoBottomLeftViewOrientation
            myDrawingViewName = ""
        Case ViewOrientationTypeEnum.kIsoBottomRightViewOrientation
            myDrawingViewName = ""
        Case ViewOrientationTypeEnum.kIsoTopLeftViewOrientation
            myDrawingViewName = ""
        Case ViewOrientationTypeEnum.kIsoTopRightViewOrientation
            myDrawingViewName = ""
        Case ViewOrientationTypeEnum.kLeftViewOrientation
            myDrawingViewName = "END"
        Case ViewOrientationTypeEnum.kRightViewOrientation
            myDrawingViewName = "END"
        Case ViewOrientationTypeEnum.kTopViewOrientation
            myDrawingViewName = "TOP"
        Case Else
            myDrawingViewName = ""
    End Select
    If Not myDrawingViewName = "" Then
        tmpView.Name = myDrawingViewName
    End If

    myDrawingViewName = ""

End If

']

End Select
End If

Next
0 Likes
Accepted solutions (1)
552 Views
8 Replies
Replies (8)
Message 2 of 9

WCrihfield
Mentor
Mentor

Hi @C_Haines_ENG.  It sounds to me like you just need to separate the drawing view's base name from its number.  Is that correct?  If you were to do so, do you want to keep/use both parts, or just the number?  In my case, the views are named something like "VIEW5", which is then followed by a colon and the file name of the model document that the view is representing, but that last part is not returned in its name, just shown in the browser.  If there were a special character between the "VIEW" part and the number, that would have made it simple & easy, but since that is not the case, I will show you one way to do it without that advantage.

Here is an example iLogic rule that will allow you to pick a drawing view, then it will show you a message displaying the view 'base name' and its 'number'.

Dim tmpView As DrawingView
tmpView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Select a View.")
If IsNothing(tmpView) Then Return
Dim oViewName As String = tmpView.Name
oVNChrs = oViewName.ToCharArray
Dim i As Integer = 0
For i = 0 To UBound(oVNChrs)
	If IsNumeric(oVNChrs(i)) Then Exit For
Next
Dim oViewBaseName As String = Left(oViewName, i)
Dim oViewNumber As Integer = CInt(Right(oViewName, (oViewName.Length - i)))
MsgBox("oViewBaseName = " & oViewBaseName & vbCrLf & _
"oViewNumber = " & oViewNumber, , "")

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 9

C_Haines_ENG
Collaborator
Collaborator

Im getting an error saying "Conversion from type Sting to Integer is not Valid.

 

Essentially its reading the label of a view and that defaults to a number so the first number of a drawing view is 1, and 2 and so on. I need the rule to differentiate between the "Typical" view names such as "Front", "Top", "Side" and the default view name being a number from 1 - Infinity. 

 

I don't want my rule to run on views that have custom names such as "ASSEM DETAIL" or "BOTTOM PANEL".

 

 

0 Likes
Message 4 of 9

WCrihfield
Mentor
Mentor

OK.  There must be something different or more complicated about your drawing view names than mine, because the code I posted worked OK in my tests.  Anyways, I think I am starting to understand what you are trying to do here.  But just so you know the 'Name' (Link1, Link2) property of a DrawingView object, and its 'Label' (Link3, Link4) property both return very different things, so when you mix the two terms it is a bit confusing.  The view's Name is what shows up in the browser, but its Label is what shows up on the drawing sheet, when have the DrawingView.ShowLabel property set to True.  I see in the code you posted you are only working with the view's Name property, which is always returned as a String value.  Although there are a few functions available that can attempt to convert or extract a numerical value from a String, it is usually a pretty messy or inaccurate process unless conditions (the contents of the String) are just right.  I am also pretty sure that it will not let you set the Name of two different drawing views in the same drawing to the same exact name.  I'm guessing that is why you want to try to add the view's number to the end of its newly assigned name...to help avoid duplicate view names.  That's a tough one.  I'm guessing you could simply ignore whatever number Inventor gave it by default, and just generate your own number within the code.  The number could increment each time you use it, so you don't use the same number more than once.  That would likely work just fine in a situation like this.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 9

C_Haines_ENG
Collaborator
Collaborator

I am looking to change the value quoted as "Label" as seen in the image below

chainesL5H3G_1-1660672646073.png

Essentially when using the tmpView.Name command it returns that value which I call the "Label"

 

If it Is A number or a typical named view I want it to run the code to switch it by whatever view cube orientation its in. 

Case "BACK", "BOTTOM", "FRONT", "SIDE", "TOP", "O/S FACE", "I/S FACE", "END", MyDrawingViewNameNUM <2000

My issue is because it returns as a string I dont really want to put a line in my code saying "1", "2", "3", "4". to about 200.

 

Is there anyway to set a true false value if tmpView.Name is a number?

0 Likes
Message 6 of 9

WCrihfield
Mentor
Mentor

OK.  Yes, the view's name is shown in that dialog box, but that is not usually the entire contents of the Label's text.  There is also usually some text showing what the scale of the view is, but the default for how that extra stuff is set-up depends on settings within the Styles & Standards / Styles Editor area.  Anyways, yes, there is a fairly simple way to tell if a String contains only numerical contents.  There is a simple vb.net function called 'IsNumeric()', where you put an 'expression' (could be different kinds of things supplied to it) into the () area, and it returns a Boolean type value.  True if it can be understood as a number.  I used that within the code I posted above, but in that example I was testing each individual character of the String, because I was expecting the view's name to start with some text, then a number at the end.  But if the view's entire name was a number, it would not have returned anything for the base name, which is probably what caused the error.

Here is a mini iLogic rule you can play around with.  As it is, it will say it IS numeric.  But if you add any letters in front of, or behind those numbers, it will report it is NOT numeric.

Dim oViewName As String = "123.456"
If IsNumeric(oViewName) Then
	MsgBox("It is numeric.", , "")
Else
	MsgBox("It is NOT numeric.", , "")
End If

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 9

C_Haines_ENG
Collaborator
Collaborator

While that does work I don't think it will work for my Case command. How could I add that to the "CASE" selection?

 

Select Case tmpView.Name
	
Case "BACK", "BOTTOM", "FRONT", "SIDE", "TOP", "O/S FACE", "I/S FACE", "END" 

 Almost as If I could add say 1 - 1000 to the end.

0 Likes
Message 8 of 9

J-Camper
Advisor
Advisor
Accepted solution

@C_Haines_ENG,

 

I cleaned up and condensed the code a little bit.  I think it is working the way you want it to:

'[ SETUP BLOCK
Dim tmpSheet As Sheet = ThisDrawing.ActiveSheet.Sheet 'Set active sheet for local iLogic rule existing inside Drawing document
Dim oView As DrawingView = tmpSheet.DrawingViews.Item(1) 'Get 1st view to determin base output
Dim oRDD As DocumentDescriptor = oView.ReferencedDocumentDescriptor

'Set list for names you want to identify. Anything matching these names will be updated
Dim masterList As New List(Of String)
masterList.AddRange({"BACK", "BOTTOM", "FRONT", "SIDE", "TOP", "O/S FACE", "I/S FACE", "END"})

'Set list of outputs based on file type
Dim useNames As New List(Of String)
If oRDD.ReferencedDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
	useNames.AddRange({"BACK", "BOTTOM", "FRONT", "ISOMETRIC", "ISOMETRIC", "ISOMETRIC", "ISOMETRIC", "SIDE", "SIDE", "TOP", ""})
ElseIf oRDD.ReferencedDocumentType = DocumentTypeEnum.kPartDocumentObject Then
	useNames.AddRange({"O/S FACE", "BOTTOM", "I/S FACE", "", "", "", "", "END", "END", "TOP", ""})
End If
']

'[ VIEW LOOP
For Each tmpView As DrawingView In tmpSheet.DrawingViews
	Dim myDrawingViewName As String = ""
	'Check name. anything NOT matching the master list or the name is NOT numbers only will get skipped
	If Not masterList.Contains(tmpView.Name) And Not IsNumeric(tmpView.Name) Then Continue For
	
	Dim oTrans As Transaction
	oTrans = ThisApplication.TransactionManager.StartTransaction(ThisDrawing.Document, "Rotate Drawing View temp")
	
		' Set rotation to 0
	If tmpView.Camera.ViewOrientationType = ViewOrientationTypeEnum.kArbitraryViewOrientation Then
		If tmpView.Rotation<>0 Then
			tmpView.Rotation = 0
		End If
	End If	
']

'[ CASE SELECTOR
	Select Case tmpView.Camera.ViewOrientationType
        Case ViewOrientationTypeEnum.kBackViewOrientation
            myDrawingViewName = useNames.Item(0)
        Case ViewOrientationTypeEnum.kBottomViewOrientation
            myDrawingViewName = useNames.Item(1)
        Case ViewOrientationTypeEnum.kFrontViewOrientation
            myDrawingViewName = useNames.Item(2)
        Case ViewOrientationTypeEnum.kIsoBottomLeftViewOrientation
            myDrawingViewName = useNames.Item(3)
        Case ViewOrientationTypeEnum.kIsoBottomRightViewOrientation
            myDrawingViewName = useNames.Item(4)
        Case ViewOrientationTypeEnum.kIsoTopLeftViewOrientation
            myDrawingViewName = useNames.Item(5)
        Case ViewOrientationTypeEnum.kIsoTopRightViewOrientation
            myDrawingViewName = useNames.Item(6)
        Case ViewOrientationTypeEnum.kLeftViewOrientation
            myDrawingViewName = useNames.Item(7)
        Case ViewOrientationTypeEnum.kRightViewOrientation
            myDrawingViewName = useNames.Item(8)
        Case ViewOrientationTypeEnum.kTopViewOrientation
            myDrawingViewName = useNames.Item(10)
        Case Else
            myDrawingViewName = useNames.Item(11)
    End Select
    If Not myDrawingViewName = "" Then
        tmpView.Name = myDrawingViewName
    End If

'Always close your Transactions
oTrans.End

Next

Instead of Select case to determine if the the name should be changed, I built a List and check the view name against that list.  To catch your numbers only view names I used the IsNumeric method that @WCrihfield  mentioned.  I also combined the actual view naming Select Case block by setting values of another list to correspond with your desired view names, per document type.

 

Let me know if you have any questions or if this is not working as intended

0 Likes
Message 9 of 9

Michael.Navara
Advisor
Advisor

Another approach is regular expressions. Here is small sample which writes to log if the view name matches its default name.

 

 

 

 

Dim drwDoc As DrawingDocument = ThisDoc.Document
'Iterate drawing views
For Each drwView As DrawingView In drwDoc.ActiveSheet.DrawingViews
	'Required and used
	Dim prefix As String
	
	'Not used variables, but are required by GetViewLabelDefaults method
	Dim visible As Boolean
	Dim formattedText As String
	Dim constrainToBorder As Boolean
	Dim placeBelowView As Boolean
	
	'Get default view prefix
	drwDoc.StylesManager.ActiveStandardStyle.GetViewLabelDefaults(drwView.ViewType, prefix, visible, formattedText, constrainToBorder, placeBelowView)

	'Use regular expression to check view name
	'Prepare regEx
	Dim pattern As String = String.Format("{0}\d.*", prefix)
	Dim regEx As New System.Text.RegularExpressions.Regex(pattern)
	
	'Check regular expression
	Dim hasDefaultName As Boolean = regEx.Match(drwView.Name).Success
	
	'Do some action
	If hasDefaultName Then
		Logger.Info("{0} default", drwView.Name)
	Else
		Logger.Info("{0} renamed", drwView.Name)
	End If
Next

 

 

 

 

0 Likes