User:CBM-c-: Difference between revisions

From Vectorworks Developer
Jump to navigation Jump to search
(expand)
(remove old rosetta stone)
Line 5: Line 5:
Ciao,
Ciao,


I am _c_, an architect and BIM Manager based in Berlin. My expertise are batch editors and powering large projects through extreme customisation. If it can be done, I get it done.
I am _c_, an architect and BIM Manager based in Berlin. With two decades of field praxis, my expertise is BIM management and project coordination, batch editors and enpowering large projects through extreme customisation. If it can be done, I get it done.
 
See www.cad-bim-manager.com for more.
See www.cad-bim-manager.com for more.


Some of you might know me as Orso from Vectorlab or the comments on the present Developer wiki.
== Vectorlab Archives ==
Here is the List Browsers article from the now dead Vectorlab site:
* List Browsers ([[:Media:Vectorlab_ListBrowsers_WebArchives.zip| Vectorlab ListBrowsers Web Archives.zip]])


I stored some of the Vectorlab sites:
== Subpages ==


== VS <> Py Rosetta Stone ==
* [[User:CBM-c-/VS-Py Rosetta Stone]]
* [[User:CBM-c-/VS-Math]]
* [[User:CBM-c-/VS-List Browsers]]


Transitioning from Vectorscript Pascal to Vectorscript Python has its challenges, here a Rosetta Stone to help you out with the task.


{| class="wikitable"
== Vectorlab Archives ==
! style="width:20%"| Description
! style="width:40%"| Vectorscript
! style="width:40%"| Python


|- style="vertical-align: top;"
Some of you know me as Orso from the now defunct Vectorlab, I maintained it for many years. Here is the List Browsers article from the former Vectorlab site:
| '''Statement ending:'''
* List Browsers ([[:Media:Vectorlab_ListBrowsers_WebArchives.zip| Vectorlab ListBrowsers Web Archives.zip]])
| '''Semicolon always needed'''
* <code>AlrtDialog('test 1') </br> AlrtDialog('test 2') { error }</code>
* <code>AlrtDialog('test 1'); AlrtDialog('test 2'); { OK }</code>
* <code>AlrtDialog('test 1') AlrtDialog('test 2') { error }</code>
| '''Semicolon needed only for multiple statements on one line:'''
* <code>vs.AlrtDialog('test 1') </br>vs.AlrtDialog('test 2') # OK</code>
* <code>vs.AlrtDialog('test 1'); vs.AlrtDialog('test 2') # OK</code>
* <code>vs.AlrtDialog('test 1') vs.AlrtDialog('test 2') # error</code>
 
Error Message: SyntaxError: invalid syntax
 
|- style="vertical-align: top;"
| '''Case sensitivity'''
| '''Not case sensitive:'''
* <code>AlrtDialog('test'); { OK }</code>
* <code>alrtDialog('test'); { OK }</code>
* <code>alrtdialog('test'); { OK }</code>
* <code>ALRTDIALOG('test'); { OK }</code>
| '''Case sensitive:'''
* <code>vs.AlrtDialog('test') # OK </code>
* <code>vs.alrtDialog('test') # error</code>
* <code>vs.alrtdialog('test') # error</code>
* <code>vs.ALRTDIALOG('test') # error</code>
 
Error Message: AttributeError: 'module' object has no attribute 'vs.ALRTDIALOG'
 
|- style="vertical-align: top;"
| '''Empty brakes for functions'''
: don't forget in python the empty brakets for routines without parameters, this rises errors that are so tricky to find.
| '''Can be without brakets'''
* <code>FSActLayer; { no paramters: brakets not needed }</code>
* <code>MySubroutine;</code>
| '''Can't be without brakets'''
* <code>vs.FSActLayer()</code>
* <code>MySubroutine()</code>
 
Error Message: - none! be careful! -
 
|- style="vertical-align: top;"
| '''Empty handles'''
: do pay attention also to the variable scope (see below).
|
* <code>h <> NIL</code>
|
* <code>h != None</code>
* <code>h != vs.Handle() # not inited instance of a handle, how cryptic</code>
 
|- style="vertical-align: top;"
| '''Variable scope'''
: perhaps the largest source of error for the vectorscripter transitioning to python
|
'''Global wins over local:'''
* Variables must be declared
* Subroutines "see" their own variables and those of any parent function/procedure where they are contained.
<code lang="pas">
{ GLOBAL ACCESS }
{ parent of subroutine "Increment" }
PROCEDURE Main;
    VAR
        { good praxis: label globals with "g" }
        gIndex, gNum : INTEGER;
   
    { subroutine }
    PROCEDURE Increment;
        BEGIN
            { gNum is not defined in this subroutine
            the parser climbs up parent containers
            until it finds a declaration for the var gNum.
            In this case it can be found in Main }
            gNum := gNum +1;
            SysBeep;
        END;
       
BEGIN
    gNum := 10; { init }
    FOR gIndex := 1 TO 10 DO
        Increment; { increments the variable gNum }
       
    AlrtDialog(Concat(gNum));
{ returns 20 }
END;
Run(Main);
</code>
|
'''Local wins over global:'''
* Variables must NOT be declared
* Subroutines create automatically a local instance of any used variable.
<code lang="py">
# LOCAL ACCESS
# subroutine
def Increment():
    # gNum is not defined in this subroutine
    # the parser creates a local instance of the var gNum!
    gNum +=1
    vs.SysBeep
gNum = 10 # init
for gIndex in range(1, 10):
    Increment
    # increments the variable gNum
    # but only inside Increment!
   
vs.AlrtDialog(str(gNum))
# returns 10! The global var didn't set
</code>
 
<code lang="py">
# GLOBAL ACCESS: CORRECT
# subroutine
def Increment():
    # gNum is not defined in this subroutine
    # tell the parser that you want to edit gNum global!
    global gNum
    gNum +=1
    vs.SysBeep()
gNum = 10 # init
# please observe that the range is NOT 1, 10!
for gIndex in range(0, 10):
    Increment()
    # increments the variable gNum
    # but only inside Increment!
   
vs.AlrtDialog(str(gNum))
# returns 20
</code>
 
<code lang="py">
# GLOBAL ACCESS: WRONG
# subroutine
def Increment():
    # gNum is not defined in this subroutine
    # tell the parser that you want to edit gNum global!
    global gNum
    gNum +=1
    vs.SysBeep()
# no init!
for gIndex in range(0, 10):
    Increment()
    # increments the variable gNum
    # but only inside Increment!
   
vs.AlrtDialog(str(gNum))
# rises error
</code>
 
Error Message: NameError: global name 'gNum' is not defined
 
|- style="vertical-align: top;"
| '''FOR statements'''
| '''Runs including last value:'''
<code lang="pas">
{ runs 3 times! 1, 2 and 3 }
FOR i := 1 TO 3 DO
    AlrtDialog(Concat(i));
</code>
| '''Runs excluding last value:'''
<code lang="py">
# runs 2 times! 1 and 2
for i in range(1, 3):
    vs.AlrtDialog(str(i))
</code>
 
|- style="vertical-align: top;"
| '''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
Error Message: - none! be careful! -
 
|- style="vertical-align: top;"
| '''Concatenate text'''
| '''Supports implicit conversion:'''
: Both [[VS:Concat| Concat]] and [[VS:Message| Message]] support multiple variable types and convert them into string.
* <code>t := Concat(10, ' fingers'); { OK }</code>
* <code>Message(10, ' fingers'); { OK }</code>
* <code>AlrtDialog(Concat(10, ' fingers')); { OK }</code>
|  '''Doesn't support implicit conversion:'''
: For example an integer won't automatically be converted into string. Wrap it in [[VS:Concat| vs.Concat]] or [[VS:Message| vs.Message]], alternatively perform the needed conversion.
* <code>t = 10 + ' fingers' # error</code>
* <code>t = vs.Concat(10, ' fingers') # OK</code>
* <code>vs.Message(10, ' fingers') # OK</code>
* <code>vs.AlrtDialog(str(10) + ' fingers') # OK</code>
* <code>vs.AlrtDialog(10 + ' fingers') # error </code>
 
Error Message: TypeError: unsupported operand type(s) for +: 'int' and 'str'
 
|- style="vertical-align: top;"
| '''Encryption'''
| Whatever .vs or .px file is linked through your includes, will be encrypted upon running the encrypt command. More infos about standard Vectorscript (Pascal) encryptions are [[VS:Include_Files_and_Encryption| here]].
| Create list of your included files in an xml file.
 
For encryption in Python there are difficulties. Instructions from Vlado on the Techboard, search for "problems-encrypting-a-python-script" (at the moment we cannot add external links to the present wiki).
 
|- style="vertical-align: top;"
| '''Python version'''
|
| <code>import sys
ver = sys.version_info</br>
vs.Message(repr(ver))</br>
</code>
 
|- style="vertical-align: top;"
| '''"import vs"'''
|
| <code>import vs</code> # do I need this?
 
|- style="vertical-align: top;"
|  '''Caching'''
: some caching prevents your script to reflect 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.''
|}
 
== Lists ==
 
Lists are powerful in Python, below some fascinating lists manipulations. They remind me of Applescript:
<code lang="py">
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'}
</code>
 
== Errors ==
Python Error Messages:
 
BaseExceptions:
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
      +-- StopIteration
      +-- ArithmeticError
      |    +-- FloatingPointError
      |    +-- OverflowError
      |    +-- ZeroDivisionError
      +-- AssertionError
      +-- AttributeError
      +-- BufferError
      +-- EOFError
      +-- ImportError
      +-- LookupError
      |    +-- IndexError
      |    +-- KeyError
      +-- MemoryError
      +-- NameError
      |    +-- UnboundLocalError
      +-- OSError
      |    +-- BlockingIOError
      |    +-- ChildProcessError
      |    +-- ConnectionError
      |    |    +-- BrokenPipeError
      |    |    +-- ConnectionAbortedError
      |    |    +-- ConnectionRefusedError
      |    |    +-- ConnectionResetError
      |    +-- FileExistsError
      |    +-- FileNotFoundError
      |    +-- InterruptedError
      |    +-- IsADirectoryError
      |    +-- NotADirectoryError
      |    +-- PermissionError
      |    +-- ProcessLookupError
      |    +-- TimeoutError
      +-- ReferenceError
      +-- RuntimeError
      |    +-- NotImplementedError
      +-- SyntaxError
      |    +-- IndentationError
      |        +-- TabError
      +-- SystemError
      +-- TypeError
      +-- ValueError
      |    +-- UnicodeError
      |        +-- UnicodeDecodeError
      |        +-- UnicodeEncodeError
      |        +-- UnicodeTranslateError
      +-- Warning
          +-- DeprecationWarning
          +-- PendingDeprecationWarning
          +-- RuntimeWarning
          +-- SyntaxWarning
          +-- UserWarning
          +-- FutureWarning
          +-- ImportWarning
          +-- UnicodeWarning
          +-- BytesWarning
          +-- ResourceWarning

Revision as of 05:08, 31 December 2020

Ciao,

I am _c_, an architect and BIM Manager based in Berlin. With two decades of field praxis, my expertise is BIM management and project coordination, batch editors and enpowering large projects through extreme customisation. If it can be done, I get it done.

See www.cad-bim-manager.com for more.


Subpages


Vectorlab Archives

Some of you know me as Orso from the now defunct Vectorlab, I maintained it for many years. Here is the List Browsers article from the former Vectorlab site: