Returning a Dictionary object from Function

Hi,

I am working on a parametric hull modeling problem. I have created a function to read parameters as name value pair from a CSV file and store them in a Dictionary object. I want to call this function from my Main() subroutine and store the dictionary object into another variable. I would like to pass this Dictionary variable to another function for further calculations.

My function looks like:

Function readHullData(FilePath, FileName)

'Creating Dictionary object to store hull data
Dim objHull
Set objHull = CreateObject("Scripting.Dictionary")

'Reading parameters from file
Dim fso, MyFile,TextLine,LineData

Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.OpenTextFile(FilePath & FileName)

'Read from the file And storing the results.
Do While MyFile.AtEndOfStream <> True
	TextLine = MyFile.ReadLine
	LineData = Split(TextLine, ",")
	
	'Storing hull data
	objHull.Add LineData(0), LineData(1)
	
Loop
MyFile.Close

'Returning the hull data
Set readHullData = objHull
End Function

And the main function from where I want to call it looks like:

Option Explicit
'Script written by Amitava Guha
'Script version Tuesday, February 3, 2015 11:14:01 AM

Call Main()

Sub Main()	
	'Parameter file location
	Dim FilePath,FileName
	
	FilePath = "C:\Users\Data\"
	FileName = "Matlab2Rhino.csv"

	'Creating Dictionary object to store hull data
	Dim objHull
	'Set objHull = CreateObject("Scripting.Dictionary")

	Set objHull = readHullData(FilePath, FileName)
End Sub

I am getting an error message Type mismatch: 'readHullData' .Can anyone tell me, how to set the function name to be an object. Also for the next stage, how to send a Dictionary object to another function. I tried the method given here, but didn’t work.

Thank you for your help.

Best,
Amitava

Dont know if it solves the problem but you can try to remove the Set part of:

'Returning the hull data
Set readHullData = objHull

So you get

readHullData = objHull

Also… is the function and the main function located in the same script?

Thank you for your response. However, this method did not solve the problem. Getting error message "Wrong number of arguments or invalid property assignment".

For the time being, I have all my functions in one file. I would like to know how I can put them separately.

I am attaching my script and the data file here. You need to rename .txt to .csv and change the path in script to make it run.

Main.rvb (4.4 KB) Matlab2Rhino.txt (592 Bytes)

I dont know why it doesnt work. If I copy the functions code and put it in the Main it works.
This is another solution. Make the objHull a script variable

Option Explicit
'Script written by Amitava Guha
'Script version Tuesday, February 3, 2015 11:14:01 AM

Call Main()

Dim objHull

Sub Main()    
    'Parameter file location
    Dim FilePath,FileName
    
    'Home Computer
    'FilePath = "C:\Users\Amitava\Dropbox\Doctor of Philosophy\Optimization\Project\Attempt_2\Data\"
    'FileName = "Matlab2Rhino.csv"
    
    'Office Computer
    FilePath = "C:\Users\Techmed\Desktop\"
    FileName = "Matlab2Rhino.csv"
    

    'Creating Dictionary object to store hull data
    'Set objHull = CreateObject("Scripting.Dictionary")
    Test FilePath, FileName
    

    
    'Call readHullData(FilePath, FileName)
    Call generateHull()
    
End Sub


'----------------------------------------------------------------------------
'Function to read CSV file and store hull data
'----------------------------------------------------------------------------


Sub Test(FilePath, FileName)
    
    'Creating Dictionary object to store hull data

    Set objHull = CreateObject("Scripting.Dictionary")
    
    'Reading parameters from file
    Dim fso, MyFile,TextLine,LineData

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set MyFile = fso.OpenTextFile(FilePath & FileName)
    
    'Read from the file And storing the results.
    Do While MyFile.AtEndOfStream <> True
        TextLine = MyFile.ReadLine
        LineData = Split(TextLine, ",")
        
        'Storing hull data
        objHull.Add LineData(0), LineData(1)
        
    Loop
    MyFile.Close
    
    '-----------------------------------------
    'Test for stored data (can be commented)
    '-----------------------------------------
    Dim arrKeys,arrItems
    arrKeys = objHull.Keys
    arrItems = objHull.Items
    
    Rhino.Print(arrKeys(0))
    Rhino.Print(arrItems(0))
    '-----------------------------------------
    
End Sub



'----------------------------------------------------------------------------
'Function to generate and save the hull surface
'----------------------------------------------------------------------------

Function generateHull()
    ' Start a new model,
    Rhino.Command "-_New n"
    
    'Set document properties
    Rhino.Command "-_DocumentProperties u u e n _enter _enter"
    
    'Create a Ship hull
    Rhino.Command "-_OrcaCreateShipHull LengthOnDeck 160" + _
        " BeamOnDeck  25" + _
        " BeamAtTransom  0.6" + _
        " BowDepth  18" + _
        " TransomDepth  18" + _
        " Draft  8" + _
        " TransomHeight  8" + _
        " DeckTaperAft  0.4" + _
        " DeckTaperFwd  0.4" + _
        " KeelRisePt  0.7" + _
        " KeelRiseRateFwd  0.3" + _
        " KeelRiseRateAft  0.2" + _
        " StemRake  30" + _
        " TransomRake  -10" + _
        " DeadriseMid  0" + _
        " DeadriseAft  0" + _
        " DeadriseFwd  0.1" + _
        " SideSlopeMid  0" + _
        " SideSlopeAft  0.1" + _
        " SideSlopeFwd  0.9" + _
        " SectionTightnessMid  0.6" + _
        " SectionTightnessAft  0.8" + _
        " SectionTightnessFwd  0.67" + _
        " FlareMid  0" + _
        " FlareFwd  0.6" + _
        " FlareAft  0" + _
        " StationMaxArea  0.5" + _
        " BowRounding  0.2" + _
        " StemCurvature  0" + _
        " ForefootShape  0.6" + _
        " Prismatic  0.25" + _
        " SheerHeight  1" + _
        " SheerHeightPos  0.5" + _
        " NumRows  9" + _
        " NumCols  12" + _
        " _enter"



    'Creating other half
    Call SelectModelGeometry
    Rhino.Command "-_Mirror p 0,0,0  1,0,0 1,0,1 _enter"
    
    'Rotating about z-axis so the bow points towards +ve x-axis
    Call SelectModelGeometry
    Rhino.Command "-_Rotate 0,0,0 180 _enter"
    
    'Translating forward to have midship at origin
    Call SelectModelGeometry
    Rhino.Command "-_Move 0,0,0 80,0,0"
    
    ' Export the selected objects
    Dim strPath,strFile
    strPath = "C:\Users\Amitava\Dropbox\Doctor of Philosophy\Optimization\Project\Attempt_2\Data\Model.igs"
    strFile = strPath
    strFile = Replace(strFile, ".3dm", "_" & "Test" & ".igs") 
    
    Rhino.Print(strFile)
    
    'Rhino.Command "-_Export " & strFile, 0
    'Rhino.Command "-_Export"
    
    Rhino.Print("Done!")
End Function

'----------------------------------------------------------------------------
' Subroutine to select all surfaces, polysurfaces, and meshes in the model
'----------------------------------------------------------------------------
Sub SelectModelGeometry()
    ' Select all surface, polysurface And mesh objects
    Rhino.Command "-_SelSrf"
    Rhino.Command "-_SelPolySrf"
    Rhino.Command "-_SelMesh"
End Sub

Decare it on the top. This works :wink:
I changed to FilePath though… Dont know the original one.

1 Like

Thank you. Defining objHull as a script variable worked. Is there a way to write the functions in separate files instead of making one long script file?

You can add an alias to a script so it is usable as a normal rhino command. After this you can use:

rhino.command yourcommandname, true

to run that rhinoscript.

1 Like