User:Orso.b.schmid: Difference between revisions

From Vectorworks Developer
Jump to navigation Jump to search
(expand)
(expand: colors)
Line 8: Line 8:


{| class="wikitable"
{| class="wikitable"
! Description !! Vectorscript !! Python
! style="width:20%"| Description
! style="width:40%"| Vectorscript
! style="width:40%"| Python
|-
|-
| Assignment operator:
| Assignment operator:
Line 15: Line 17:
|-
|-
| Empty handle:
| Empty handle:
| <code>h <> NIL</code>
|  
| <code>h != None</code>
* <code>h <> NIL</code>
<code>h != vs.Handle() # not inited instance of a handle, how cryptic</code>
|  
* <code>h != None</code>
* <code>h != vs.Handle() # not inited instance of a handle, how cryptic</code>
|-
| Colors:
|
* Color Index:
*: <code>SetPenFore(h, RGBToColorIndex(65535, 0, 0));</code>
*: <code>PenFore(RGBToColorIndex(65535, 0, 0));</code>
* RGB:
*: <code>SetPenFore(h, 65535, 0, 0);</code>
|
* Color Index:
*: <code>vs.SetPenFore(h, vs.RGBToColorIndex(65535, 0, 0)) </code>
* RGB in Tuple:
*: <code>vs.SetPenFore(h, (65535, 0, 0)) </code>
* Hex in Tuple:
*: <code>vs.SetPenFore(h, (0xFFFF, 0, 0))</code>
 
Warning: don't forget the brakets:
* <code>vs.PenFore((65535, 0, 0)) </code> correct
* <code>vs.PenFore(65535, 0, 0) </code> fails
|-
|-
| Concatenate text:
| Concatenate text:
| <code>Concat(text1, ' ', text2)</code>
|  
| <code> text1 + ' ' + text2</code>
* <code>Concat(text1, ' ', text2)</code>
|  
* <code> text1 + ' ' + text2</code>
|-
|-
| Used Python version:
| Used Python version:
Line 30: Line 55:
</code>
</code>
|-
|-
| Declaring "VS" yes or no?
| "import vs" yes or no?
|  
|  
| <code>import vs:</code>
| <code>import vs</code> # do I need this?
|-
|-
| There is some caching going on preventing your script to reflect your changes:
| There is some caching going on preventing your script to reflect your changes:

Revision as of 05:17, 19 May 2015

Ciao,

I am Orso, an Italian Vectorscripter since many years. Some of you might know me from Vectorlab or the comments on the present Developer wiki. I feel very comfortable with Vectorscript (from now on: VS) but will now switch over to Python for even more power. I will try to share here comments, problems -and solutions- from the point of view of a non-programmer. --Orso.b.schmid (talk) 08:13, 17 May 2015 (EDT)

If you add comments, please use the full wiki formatting, easily available clicking on Advanced while on edit mode and don't forget to sign up your comment using --~~~~!

VS <> Py FAQ

Description Vectorscript Python
Assignment operator: := =
Empty handle:
  • h <> NIL
  • h != None
  • h != vs.Handle() # not inited instance of a handle, how cryptic
Colors:
  • Color Index:
    SetPenFore(h, RGBToColorIndex(65535, 0, 0));
    PenFore(RGBToColorIndex(65535, 0, 0));
  • RGB:
    SetPenFore(h, 65535, 0, 0);
  • Color Index:
    vs.SetPenFore(h, vs.RGBToColorIndex(65535, 0, 0))
  • RGB in Tuple:
    vs.SetPenFore(h, (65535, 0, 0))
  • Hex in Tuple:
    vs.SetPenFore(h, (0xFFFF, 0, 0))

Warning: don't forget the brakets:

  • vs.PenFore((65535, 0, 0)) correct
  • vs.PenFore(65535, 0, 0) fails
Concatenate text:
  • Concat(text1, ' ', text2)
  • text1 + ' ' + text2
Used Python version: import sys

ver = sys.version_info
vs.Message(repr(ver))

"import vs" yes or no? import vs # do I need this?
There is some caching going on preventing your script to reflect your changes: varPersistentPythonEngine = 412 { Boolean }

In the SDK starting from VW 2014 we can read: When True the Python engine is the same for the execution of all scripts, this solves some issues with Py_Initialize and Py_Finalize. For example, when debugging externally python leaves threas that cause crash if Py_Initialize and Py_Finalize is used for each script call. So, this allows the engine to be preserved between calls, however Vectorworks will delete all custom modules and objects defined in the engine prior each execution.

Encryption: Whatever .vs or .px file is linked through your includes, will be encrypted upon running the encrypt command. More infos here. Create list of your included files in an xml file. Please read Vlado on Techboard

Lists

Lists are powerful in Python, below some fascinating lists manipulations. They remind me of Applescript:

months = "Jan Feb Mar Apr May Jun Jul"
months = months.split() # no splitter defined and it will use the empty space --> ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
months[2] # --> 'Mar' note that the index is 0-based
months2 = "Jan, Feb, Mar, Apr, May, Jun, Jul"
months2.split(', ') # --> ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul'] use comma and empty space as splitter 
months.append('Jul') # --> ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul'] append adds an item to a list 
months.pop() #- -> 'Jul' pop fetches the last item of a list
', sunny '.join(months) # --> ', sunny Jan, sunny Feb, sunny Mar, sunny Apr, sunny May, sunny Jun, sunny Sep'
'-'.join(months[1:3]) # --> 'Feb-Mar'
del months[2] # --> ['Jan', 'Feb', 'Apr', 'May', 'Jun', 'Jul']
months = {1: 'Jan', 2: 'Feb', 3: 'Mar'} # --> {1: 'Jan', 2: 'Feb', 3: 'Mar'}

Increment a var

So far I have spent a really inordinate amount of time trying to increment a counter from within a calllback routine. In VS one does it like this:

cnt := cnt +1; { variable cnt is incremented }

For example:

PROCEDURE Test;
VAR
    cnt : INTEGER; { variable scope here is global for this script }

    { callback subroutine fitting ForEachObject }
    PROCEDURE DoSomething(h: HANDLE);
        BEGIN
            { ... do something }
            cnt := cnt +1; { global variable is incremented }
        END;
BEGIN
    cnt := 0; { explicit is better than implicit :) }
    ForEachObject(DoSomething, (ALL)); { pick objects by criteria, there the variable cnt will increment }
    AlrtDialog(Concat('Did something ', cnt, ' times.'));
END;
Run(Test);

How do I get this done in Python? It's not as easy as it looks like. I thought that this would work:

cnt += 1

For example:

import vs;
cnt = 0 # explicit is better than implicit :)

def DoSomething(h):
    # ... do something
    cnt += 1 # variable should be incremented

vs.ForEachObject(DoSomething, '(ALL)')  # pick objects by criteria, there the variable cnt will increment
vs.AlrtDialog(vs.Concat('Did something ', cnt, ' times.'))
Error: UnboundLocalError: local variable can't be referenced before assignment

But it doesn't work from within DoSomething and I can't turn DoSomething into a function outputting an integer, otherwise it won't fit the required callback syntax expected by ForEachObject

Searching the web I found out that I am not alone in this misery. See this, LOL. This comment wins, on my opinion: "We can make Python ask Perl to ask C."

Now I'll try this mysterious approach treating the variable as a list (http://bytes.com/topic/python/answers/46419-how-does-one-write-function-increments-number source):

def incr(counters): counters[0] += 1

counters =[100]
incr(counters)
print counters [101]