ilogic: 'thisdoc' is not declared... inside a class. Do I need an import or reference?

ilogic: 'thisdoc' is not declared... inside a class. Do I need an import or reference?

benny.deswert
Enthusiast Enthusiast
768 Views
4 Replies
Message 1 of 5

ilogic: 'thisdoc' is not declared... inside a class. Do I need an import or reference?

benny.deswert
Enthusiast
Enthusiast

Hi all.

I am working with a vb form instead of a ilogic form. Reasing being that I want to have adaptible number of checkboxes to represent the number of views in a drawing. I have an example that I found online to make the forms, wich all works fine.

 

The problem I have is that there are some things inside the code I Haven't used before like "public class", "public sub" etc..

 

I have made a simple function that just returns the number of views on a drawing. The idea is then that the form only shows as much checkboxes as there are views. And also in a later stage the description would match the view.

The problem is that if I call it from one class it works. But if I put it in the other class it does not and returns 'thisdoc' is not declared.

I'm guessing I need to add an import or a reference somewhere, but I don't know what and I don't know where.

 

The getviews function works, the getviews2 function gives the error.

 

Any help is much appreciated.

Public Class RunMyForm
	
	Private Sub Main
		
	MsgBox (getviews)
	
			' Create new instance of the custom form
	        Dim myfrm As New CustomForm
			
			' Use ShowDialog so it waits for user / form close before continuing
			'MsgBox(myfrm.ShowDialog())
			
			Dim oResult As CustomForm.CustomDialogResult = myfrm.ShowDialog
			
			' Display the custom form result
		'	MessageBox.Show(oResult)
	 End Sub
		
Function getviews As Integer	
	Dim odoc As Document
	Dim oDrawingDoc As DrawingDocument
	oDrawingDoc = ThisDoc.Document
	getviews = 0
	
	For Each oView In oDrawingDoc.ActiveSheet.DrawingViews
		getviews = getviews + 1
		'MsgBox(oView.ReferencedDocumentDescriptor.ReferencedDocument.displayname)
	Next
End Function	
	
End Class



Public Class CustomForm
	
	Inherits System.Windows.Forms.Form
	
	Public myResult As String = "TestResultHere"
	Public myProp1 As String = "TestProperty"

Function getviews2 As Integer	
	Dim odoc As Document
	Dim oDrawingDoc As DrawingDocument
	oDrawingDoc = ThisDoc.Document
	getviews2 = 0
	
	For Each oView In oDrawingDoc.ActiveSheet.DrawingViews
		getviews2 = getviews2 + 1
		'MsgBox(oView.ReferencedDocumentDescriptor.ReferencedDocument.displayname)
	Next
End Function
	
	Public Sub New()
		' This is run when a new instance of the form is created	
		
		oForm = Me
	
		With oForm
			' Set up form	
			.FormBorderStyle = FormBorderStyle.FixedToolWindow 'Fixed3D / FixedDialog / FixedSingle / FixedToolWindow / None / Sizable / SizableToolWindow
			'.MaximizeBox = False
			'.MinimizeBox = False
			.StartPosition = FormStartPosition.CenterScreen
			.Width = 600
			.Height = 500
			.TopMost = True
			.Text = "Custom Form"
			.Name = "Custom Form"
			
		End With
		
		Dim Button1 As New Button()
		With Button1
			'.Font = myfnt
			.Text = "test"
			'.Width = 100
			'.Height = 100
			.Top = 25
			.Left = 25
			.Enabled = True
		End With
		
		
		Dim i As Integer = 0
Dim offset = 10

Do While i <> 5
	i = i + 1
	Dim checkbox = New CheckBox()
	oForm.Controls.Add(checkbox)

  checkbox.Top = 100 + i*20
  checkbox.Left = 50
  checkbox.Text = "checkbox" & i
  checkbox.Checked = True
  
Loop
		'Add your Event handler
		AddHandler Button1.Click, AddressOf Button1_Click  
			
		oForm.Controls.Add(Button1)	
	
		' Assign dialog result for the form (note this returns 0 based index of the enumerator
		Me.DialogResult = CustomDialogResult.Slope4
		'MessageBox.Show(GetGroupBoxCheckedButton(oGB).Text)
		
	End Sub


	Private Sub CustomForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
	    If MessageBox.Show("Are you sure to close this application?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = System.Windows.Forms.DialogResult.Yes Then
		
	    Else
	      e.Cancel = True
	    End If
	 End Sub
  
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal E As System.EventArgs)
		myResult = "testing clicked button"
		'sender.parent.Close ' Accesses the form and closes after button click
		' Can also direct cast to object "DirectCast"
		Dim ores As CustomDialogResult = CustomDialogResult.Slope2
		MessageBox.Show(ores.ToString)
		
	End Sub
	
	

	' Create a custom enumerator for windows for result
	Public Enum CustomDialogResult
	        Slope1
	        Slope2
	        Slope3
	        Slope4
	End Enum

End Class
	

 

 

 

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

Michael.Navara
Advisor
Advisor

This is because you use classes as objects. Everything the class needs, you must push to them.

You can use

  1. Method argument - useful when you want to call the same function to different documents
  2. Class variable - useful when you want to work with the same document and call different functions on them

See the commented sample below. It displays document display name several times.

 

 

Sub Main()

    Dim doc As Document = ThisDoc.Document

    'This works, because ThisDoc is declared on rule class
    MsgBox(ThisDoc.Document.DisplayName)

    'Use of class DocumentTools sample
    Dim documentTools As DocumentTools = New DocumentTools

    'Do nothing, because content of method is commented out
    documentTools.ShowDisplayName_DoesntWork()

    'Display current document display name
    documentTools.ShowDisplayName_UseArgument(doc)

    'This call fail with NullReferenceException
    'documentTools.ShowDisplayName_UseClassVariable()
    
    'You need to set class variable first
    documentTools.docInClass = doc
    documentTools.ShowDisplayName_UseClassVariable()

End Sub


Class DocumentTools

    Public Sub ShowDisplayName_DoesntWork()
        'This can't be compiled
        'because ThisDoc is not declared in class DocumentTools
        'MsgBox(ThisDoc.Document.DisplayName)
    End Sub

    Public Sub ShowDisplayName_UseArgument(docArgument As Document)
        MsgBox(docArgument.DisplayName)
    End Sub

    Public docInClass As Document

    Public Sub ShowDisplayName_UseClassVariable()
        MsgBox(docInClass.DisplayName)
    End Sub

End Class

 

 

0 Likes
Message 3 of 5

WCrihfield
Mentor
Mentor

Hi @benny.deswert.  It's a little difficult to explain, but the term 'ThisDoc' is declared as an ICadDoc Type object (represents an Interface unique to the iLogic add-in) automatically, within the Class that it also automatically created for you called 'ThisRule'.  And If you do not include your own "Sub Main" & "End Sub" around your simple codes, it automatically creates that for you too in the background.  These are all part of what makes iLogic easier for non-programmers to use, because it takes care of a lot of details for you automatically, behind the scenes.  So, in a new custom Class, that term 'ThisDoc' has not been declared, has no Type, and has no Value.  That ICadDoc interface is defined within the Autodesk.iLogic.Interfaces resource.  Maybe try freshly declaring that variable within your other custom Class to make it available within it.

Try adding this:

 

Implements ICadDoc

 

 on the next line after declaring your custom Class.  I think this introduces its use within the Class.

Then try declaring the variable within your new custom Class.

Dim ThisDoc As New ICadDoc

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 5

JelteDeJong
Mentor
Mentor
Accepted solution

I slimmed down your code but this is how you could use ThisDoc inside your custom class.

Sub Main()

    Dim myfrm As New CustomForm(ThisDoc)
    myfrm.ShowDialog()

    MsgBox(myfrm.NumberOfViews)

End Sub


Public Class CustomForm
    Inherits System.Windows.Forms.Form

	' Add the ThisDoc to your custom class
    Private ThisDoc As ICadDoc

    Function getviews2() As Integer
        Dim oDrawingDoc As DrawingDocument = ThisDoc.Document
        NumberOfViews = oDrawingDoc.ActiveSheet.DrawingViews.Count
    End Function

    Public Sub New(thisDoc As ICadDoc)
        ' This is run when a new instance of the form is created	

		' set the ThisDoc so we can use it inside the class
        Me.ThisDoc = thisDoc

        Dim oForm = Me

        With oForm
            .FormBorderStyle = FormBorderStyle.FixedToolWindow
            .Width = 600
            .Height = 500
            .TopMost = True
            .Text = "Custom Form"
            .Name = "Custom Form"

        End With

		' do calculation
        getviews2()
    End Sub

    Public Property NumberOfViews As Integer

End Class

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 5 of 5

benny.deswert
Enthusiast
Enthusiast

Hi guys.

 

Sorry for the late reply. Had some other urgent stuff to do.

Thank you all for the explanations, it cleard up alot.

Got it working using

Private thisdoc As ICadDoc

Thx! 

0 Likes