VS:PythonExecute: Difference between revisions

From Vectorworks Developer
Jump to navigation Jump to search
No edit summary
 
No edit summary
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{LocationMain|category=LocationVS|specific=}}
__TOC__
__TOC__
<vwDoc>
<vwDoc>
Line 5: Line 5:
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
<desc>
<desc>
Execute the given python script.<BR>
Execute the given python script.
<BR>
 
You can use 'include' in python to run additional code in python files. However, before you execute the script make sure the python file are foundable through the PythonGetSearchPath. Use PythonSetSearchPath to change it.<BR>
You can use 'include' in python to run additional code in python files. However, before you execute the script make sure the python file are foundable through the PythonGetSearchPath. Use PythonSetSearchPath to change it.
<BR>
 
Scripts executed via this function should not contain User Interactive functions like GetPt for example.</desc>
; Note: Scripts executed via this function should not contain User Interactive functions like GetPt for example.</desc>


-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
Line 34: Line 34:


-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
<remark></remark>
<remark>
</remark>


-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
<sample></sample>
<sample>
An example of a simple VectorScript that runs a python script:
<code lang='vs'>
PROCEDURE Example;
VAR
  strName : STRING;
BEGIN
  strName := 'vs string';
 
  PythonBeginContext;
  PythonExecute( 'import vs' );
  PythonExecute( 'ss = ''hello from python: ''' );
  PythonExecute( 'vs.AlrtDialog(ss + vs.GetVSVar("strName"))' );
  PythonExecute( 'vs.SetVSVar("strName", "python string")' );
  PythonEndContext;
 
  AlrtDialog( Concat( 'VectorScript received: ', strName ) );
END;
RUN(Example);
</code>
 
You can use PythonExecute to send a Python script to VW via AppleScript:
<code lang='py'>
    tell application "BBEdit"
        set thePyScript to contents of front window
        set thePyScriptLines to paragraphs of thePyScript
        set theScript to "
            Procedure RunPython;
            VAR
                pyScript :DYNARRAY [] OF CHAR;
     
            BEGIN
            pyScript := '';
            PythonBeginContext;
    "
        repeat with i from 1 to number of items in thePyScriptLines
            set scriptLine to item i of thePyScriptLines
            set theScript to theScript & "pyScript := Concat(pyScript, '" & scriptLine & "', '" & return & "');" & return
        end repeat
   
        set theScript to theScript & "
            PythonExecute(pyScript);
            PythonEndContext;
            END;
            Run(RunPython);
            "
    end tell
   
    tell application "Vectorworks 2014"
        activate
        DoScript theScript
    end tell
</code>
</sample>


-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
Line 44: Line 98:
[[VS:PythonGetSearchPath]]  
[[VS:PythonGetSearchPath]]  
| [[VS:PythonSetSearchPath]]  
| [[VS:PythonSetSearchPath]]  
| [[VS:PythonBeginContext]]
</seeAlso>
</seeAlso>



Latest revision as of 19:25, 25 August 2017

.VectorScript|VectorScript ..VS:Function Reference|Function Reference ..VS:Function_Reference_Appendix|Appendix

Description

Execute the given python script.

You can use 'include' in python to run additional code in python files. However, before you execute the script make sure the python file are foundable through the PythonGetSearchPath. Use PythonSetSearchPath to change it.

Note
Scripts executed via this function should not contain User Interactive functions like GetPt for example.
PROCEDURE PythonExecute(
script :DYNARRAY[] of CHAR);
def vs.PythonExecute(script):
    return None

Parameters

script DYNARRAY[] of CHAR The script to be executed.

Example

An example of a simple VectorScript that runs a python script:

PROCEDURE Example;
VAR
  strName : STRING;
BEGIN
  strName := 'vs string';
  
  PythonBeginContext;
  PythonExecute( 'import vs' );
  PythonExecute( 'ss = ''hello from python: ''' );
  PythonExecute( 'vs.AlrtDialog(ss + vs.GetVSVar("strName"))' );
  PythonExecute( 'vs.SetVSVar("strName", "python string")' );
  PythonEndContext;
  
  AlrtDialog( Concat( 'VectorScript received: ', strName ) );
END;
RUN(Example);

You can use PythonExecute to send a Python script to VW via AppleScript:

    tell application "BBEdit"
        set thePyScript to contents of front window
        set thePyScriptLines to paragraphs of thePyScript
        set theScript to "
            Procedure RunPython;
            VAR
                pyScript :DYNARRAY [] OF CHAR;
       
            BEGIN
            pyScript := '';
            PythonBeginContext;
    "
        repeat with i from 1 to number of items in thePyScriptLines
            set scriptLine to item i of thePyScriptLines
            set theScript to theScript & "pyScript := Concat(pyScript, '" & scriptLine & "', '" & return & "');" & return
        end repeat
    
        set theScript to theScript & "
            PythonExecute(pyScript);
            PythonEndContext;
            END;
            Run(RunPython);
            "
    end tell
    
    tell application "Vectorworks 2014"
        activate
        DoScript theScript
    end tell

Version

Availability: from Vectorworks 2014

See Also

VS Functions:

VS:PythonGetSearchPath | VS:PythonSetSearchPath | VS:PythonBeginContext

VS Functions:

[[VS:PythonGetSearchPath]] | [[VS:PythonSetSearchPath]]

| [[VS:PythonBeginContext]]