Convert VB to Python

ghpython
grasshopper
convert
vbscript

(Gpetrica001) #1

hi, I am trying to translate a certain code from vb to python .
More precisely it is a circle packing code which can be found her : https://tobias-schwinn.net/2010/03/08/a-spiral-approach-to-circle-packing/ .
Given that I am quite new to scripting language overall, I wonder whether there is any sort of guides or anything of a kind that would help me understand better the used syntaxes. I did search quite a bit, but the syntax use seem to be rather complex so I found myself not being able to know where to start with the conversion.

I did also try a few online vb script to python or even to C#(in case of the slight chance it would be a bit clearer for me) yet it mostly resulted in errors.

Therefore are there any pertinent guides or anything of a kind that would make my conversion easier or actually “do-able” ?

Thank you for any feedback, all the best

I am well aware that there may be a lot of mistake in the attempt to translate the script, but I am quite new to scripting yet I kind off need to flashforward learning python as it is required for a school project I am working on
CirclePacking.gh (17.1 KB)


#2

These may be of assistance since you say you are new to scripting.




(Gpetrica001) #3

Thank you for your answer. ^^

I did read the python primer. And I am still looking at it as well as the vb “guides” and examples. Yet I am still confused if referring to certain lines such as:

Dim intCurrCenter As Int32 = 0
Dim oCenterCircle As OnCircle = Circle_List.Item(intCurrCenter)


#4

Dim is just vb variable declaration.

intCurrCenter is being declared as a variable of 32 bit integer type (Int32) and being set to 0.

I don’t know what the second one is (I’ve never programmed in VB, only vbscript), but it looks like a variable (oCentreCircle) of class OnCircle, whatever that is.


(Gpetrica001) #5

oh I thought that the OnCircle was a list :sweat_smile:

hm, weirdly don’t remember reading anything similar to the 32 bits integer set to a number ( If I am not wrong, this operation is to locate the item 0 from a list, more like the first point from the recovered items)


#7

(caveat, I am not an expert at this)

It looks like oCentreCircle is being set as a class of OnCircle with a value from a list Circle_List. The item is whatever inCurrCenter is set to.


(qythium) #8

Whoa, looks like that definition dates back 8 years ago and uses a legacy version of the VB scripting component which directly references the C/C++ OpenNURBS library…


(newer version of the component shown below)

From what I understand, the classes OnCircle, On3dVector etc. would be the underlying lower-level constructs that are wrapped inside the easier-to-use RhinoCommon library for modern components. The latter is what GhPython uses when you import Rhino at the top of your script
http://developer.rhino3d.com/api/RhinoCommon/html/N_Rhino.htm

You’d have to find the equivalent RhinoCommon classes and methods to implement the code logic, or even as the rhinoscriptsyntax wrappers to RhinoCommon for an even more beginner-friendly alternative:
http://developer.rhino3d.com/api/RhinoScriptSyntax/

Is that enough to go on? I’m not very familiar with VB myself but could take a shot at it later - If it’s just general circle packing you’re trying to achieve, I think there are other more recent scripts and definitions on the forums you can search for. :slight_smile:


(Gpetrica001) #9

Thank you for your answer !! :slight_smile: damn, I didn’t even realise it was an oldversion of vb. :smiley: now it makes sence why I couldn’t figure anything out .
Well, I guess I’ll just try to recreate the acode other ways.

I did look for scripts but this one is as close to what I need in terms of how it creates the circle packing. ( I did try a decent numbers of grasshopper definition including the use of kangaroo, but the end result wasn’t satisfying. Hence the reason I decided to try and script it in order to have more control over it) . But I didn’t really find any python script except a tutrial from http://designalyze.com/intro-scripting-python-rhino/intro-python-scripting-17-simple-growth-01 except that I need to create the circles or spheres using the math formula present in the vb instead of the way he uses :slight_smile: which I am currently trying to do, hoping that I’ll able to achieve the desired result :slight_smile:


(qythium) #10

Here you go :slight_smile:
CirclePacking_edited.gh (11.7 KB)

The circle packing “tree” looked interesting so I had a go at mechanically translating the VB script logic to Python- the result is definitely not good Python style but it works. ( original code used a Goto statement! :scream:)

Kinda disappointed to find out the algorithm basically brute-forces the circle packing, and runs in quadratic time ( you really don’t want to use it on larger numbers of circles )


(Gpetrica001) #11

Awesome !!! Thank you soo much ! :slight_smile:

It is way clearer than the old VB :smiley:

well it is a recursive code, only thing I’m going to try is create the packing is do a sort of generation of the circles instead of “rearranging” them. But I still want to use K. Steephenson’s equation since I need the circles to be “perfeclty” tangent. Thanks again! you’re the best ! :slight_smile:


(Gpetrica001) #12

I wonder whether it would be possible to limit the circle’s radius to 1 or 2 values. Otherwise having all circles with the same radius, or the radius being either 5 or 6 for example. I did try certain things withing grasshopper, but I didn’t succed for now.


(qythium) #13

The logic determining the circle radii is entirely outside of the script - you’ll want to edit/replace these components on the left?


(Gpetrica001) #14

yeah, I actually figured it out :slight_smile: ty