Jump to content
  • 0

Improved scripting support


Will

Question

I'd like much better support for scripting. The ruby scripting API for google sketchup is light years ahead of the pascal scripting system used by VW.

See here:

https://developers.google.com/sketchup/docs/tutorial_geometry

For example, here's a bit of code I wrote which allows you to right click on a component which you wish to exchange for another component and select the new component to use by simply clicking an instance of it in the drawing.

Heres the code which makes the swap:

def swap_components(theDef)
 ents = Sketchup.active_model.selection
 ents.each { |ent| ent.definition = theDef }
end

Heres the code which handles adding a menu option to the UI


class SwapTool
 # The activate method is called by SketchUp when the tool is first selected.
 def activate
   Sketchup::set_status_text("Select replacement component", SB_PROMPT)
 end

 def deactivate(view)
   view.invalidate
 end

 # The onLButtonUp method is called when the user releases the left mouse button.
 def onLButtonUp(flags, x, y, view)
   ph = view.pick_helper(x, y) #pick helper deals with working out what you have clicked on
   ph.do_pick(x, y)
   entity = ph.best_picked
   return nil if not entity.kind_of?(Sketchup::ComponentInstance) #if the thing thats clicked on is not a component then do nothing.
   Sketchup.active_model.start_operation("Swap components") #tells sketch up that all the next commands will be one undo operation
   swap_components(entity.definition) # calls the swap_components method
   Sketchup.active_model.commit_operation #finishes the undo operation
   Sketchup.send_action("selectSelectionTool:") #tells sketch up to activate the select tool
 end

 def onCancel(flag, view)
   Sketchup.send_action("selectSelectionTool:")
 end
end # class SwapTool

As you can see it's pretty simple and concise compared to pascal.

You could possibly use swig to generate bindings to your existing C++ classes automatically

http://www.swig.org/

Perhaps you could also use QT for the user interface.

http://qt.nokia.com/products/

I'd be happy with ruby, python, or any modern object oriented scripting language.

Kind Regards,

Will

Link to comment

13 answers to this question

Recommended Posts

  • 0

I don't know yet, I'm looking into it and I'll paste the code here if I get around to figuring it out. The point I was making was more about the constraints of a purely procedural language like pascal and the lack of modern scripting features like being able to write reusable modules, support for 'objects' (in the coding sense of the word) and 'duck typing' which makes scripts much easier to write.

E.g. if ruby was fully supported, you would have access to the huge number of libraries (ruby 'gems'), so it would be very easy to access and manipulate web API's like the dropbox API. So it would be easy to write some code to issue drawings to an online service like dropbox.

I think rhino has recently added the option of writing python code, but I've never used it. Neither autoCAD or microstation has particularly good or well documented scripting support though. I tinkered with visual basic in microstation and it was pretty painful. I found it easier to tinker with microstation in C# than VB.

Link to comment
  • 0

This can also be done in VS so it is just a matter of syntax and know how.

You can practically do anything within the VW environment with VS so there is really no urgency to replace it. If you want to interface with the OS then you can use the SDK which uses C++ as the (OOP) programming language. This is the same scheme that Microstation has (VB for scripting and C for more complicated apps).

The reason pascal is the programming language has to do with the origins of the program. Like the Mac, all the apps developed for it, including MiniCad (VW), were done in pascal. When the Mac and VW where converted to the C language, the SDK was developed as the modern and more complete alternate to VS.

What lacks is a better programming interface, like visual dialogs, syntax checking, etc., and better documentation, specially for using the SDK on Windows which is a very complicated process just to set it up.

Link to comment
  • 0

C++ programming is too complicated to just pick up and use for quickly solving small problems. You need to program C++ as your day job for years to really be able to use it properly. It took me a couple of hours to create my first useful Ruby Sketchup script.

The nice thing about using a modern language for scripting instead of Pascal is that there is masses of learning material on the internet and the debuggers, syntax checking, autocomplete etc is all done already and well supported by third party IDEs. Instead of using resources developing their own IDE for Pascal within Vectorworks, Nemetschek could supply plugins for Eclipse, Textmate, or whatever to enable features like syntax highlighting and autocomplete for the Vectorworks API. Ruby / Python are open source MIT style licenses so the source code can be incorporated into a commercial app. Tools such as SWIG can be used to automatically generate bindings to the Vectorworks C++ object model so it's not such a big a task to integrate one of these into VW.

Cinema4d has a Python scripting interface, as does Modo, Rhino and Blender. Sketchup has Ruby. Solidworks, Microstation, Rhino and AutoCAD can all be programmed with any .NET supported language which means as well as VB you can use C# which is a bit more complicated than Ruby/Python but is still pretty nice to work with and has memory management and OOP. On the other hand Vectorworks is struggling along with this slow and clunky pascal thing which no-one programs in anymore because it stifles creativity.

As to it just being a matter of syntax and know how. Well C++ is just a matter of syntax and know how. Assembly code is a matter of syntax and know how... Why would Apple have bothered to switch from PASCAL to C and Objective-C if Pascal was good enough. Why would Microsoft have bothered to create C# and the CLR if C++ was good enough.

Once you have a modern flexible programming interface you can add features much more quickly (quite a few sketchup features are ruby plugins) and 3rd parties would be a lot more enthusiastic about developing for the VW cad platform.

Edited by Will
Link to comment
  • 0

OK so here's a concrete example. I wanted to rename classes that started with two digits and an underscore. e.g. a class called '04_LANDFORM' would be renamed but 'Notes-Title block' would not.

Heres the pascal code to achieve this in Vectorworks:

PROCEDURE Main;
VAR
className :STRING;
index, classCount : LONGINT;
firstChar, secondChar : BOOLEAN;

FUNCTION IsInteger(theChar:STRING):BOOLEAN;
VAR
asciiNum : INTEGER;
BEGIN
{ascii codes 48 to 57 correspond to 0 to 9}
asciiNum := Ord(theChar);
IF ((asciiNum > 47) AND (asciiNum < 58)) THEN
	IsInteger:= TRUE
ELSE
	IsInteger:= FALSE;
END;

BEGIN
classCount := ClassNum;
FOR index := 1 TO classCount DO BEGIN
	className := ClassList(index);
	firstChar := IsInteger(Copy(className, 1, 1));
	secondChar := IsInteger(Copy(className, 2, 1));

	IF ((firstChar) AND (secondChar)) THEN
		RenameClass(className, Concat('Site-OS-',className));

END;
END;
RUN(Main);

And here's the equivalent to rename layers in sketch up

Sketchup.active_model.layers.each{|item| item.name = ("Site-OS-" + item.name) if item.name[/^\d{2}_/] }

I didn't manage to recreate my swap tool yet because I haven't had time but I will do this eventually.

Edited by Will
Link to comment
  • 0

Dieter,

You proved my point when I said it takes some know how to code more efficiently and you cannot blame it entirely on the choice of language.

To shorten it, you coudl also use

IF Pos('_',className) > 0 THEN

....

which removes the need for the IsInteger function.

or copy the first two characters, convert to a number and test for greater than 0.

In general, it does not matter the language but what VW offers to accomplish a task. As an example, when the DTM was introduced as a hybrid symbol, I had to make my own routine to find the elevation at a given x,y coordinate, which was over a 100 lines. Now, VS has one function that will do the same task.

Link to comment
  • 0

You proved my point when I said it takes some know how to code more efficiently and you cannot blame it entirely on the choice of language. In general, it does not matter the language but what VW offers to accomplish a task.

Totally true. As a programmer, you always have to learn new faster, better ways of doing stuff, in any language. On the other hand, I also think that it's time that Vectorscript got an update to be just a bit more flexible and open, or that we can program scripts in other .net languages other then c++. C++ isn't learned at school anymore and it is considered an old language you better not start your programs in. It's only used because programs are still developed that where started in it. It can't be hard to let us program in c# for example, because the whole .net design is build in a way that it doesn't matter which language you use, you can easily combine vb with c#.

Link to comment
  • 0

Miguel,

I'm just learning Vectorscript so I'm grateful for any feedback, here's the updated script based on your comments.

PROCEDURE Main;
VAR
className :STRING;
index, classCount : LONGINT;
numTest, uscoreTest: BOOLEAN;
returnedNumber :REAL;

BEGIN
classCount := ClassNum;
FOR index := 1 TO classCount DO BEGIN
className := ClassList(index);
numTest := ValidNumStr(Copy(className, 1, 2), returnedNumber);
uscoreTest := Pos('_', className) = 3;

IF ((numTest) AND (uscoreTest)) THEN
RenameClass(className, Concat('Site-OS-',className));

END;
END;

RUN(Main);[/Code]

I think you have to admit that it's still pretty clunky though. By the way, I noticed in your comment above that you call the Pos() function in the conditional. My script doesn't compile if I call a function in the conditional statement, I'm not sure why this is, seems like an unusual constraint. I tried googling for help on this but didn't get anything. Is this expected?

Your point about the API being more important than the language is true, but if you look at the standard libraries for Ruby, Python, C# or whatever they provide lots of useful general functionality which Vectorscript is lacking, quite a lot of the Vectorscript API is replicating stuff thats available by default in the standard library of mainstream languages. In the long run, surely it would be less effort to deprecate Vectorscript and start afresh with only the API needed to provide CAD and BIM functionality?

Edited by Will
Link to comment
  • 0

DWorks,

.NET is tricky on the mac. Rhino has a .NET API but they haven't managed to port it over to the mac version of Rhino yet because they can't work out how to do it. Probably it would be simpler to interface with one of Python or Ruby or another open source language.

W

Link to comment
  • 0

My script doesn't compile if I call a function in the conditional statement, I'm not sure why this is, seems like an unusual constraint. I tried googling for help on this but didn't get anything. Is this expected?

In a multiple conditional statement, you have to enclose each conditional in parenthesis as in:

IF (boolean expression) & (boolean expression) THEN ....

Otherwise, the compiler does not know where the conditional expression ends.

Your point about the API being more important than the language is true, but if you look at the standard libraries for Ruby, Python, C# or whatever they provide lots of useful general functionality which Vectorscript is lacking

You are comparing oranges to apples. Vectorscript is intended to automate and build plug-ins within VW only and it does a very good job at providing this limited functionality. For extended functions and access to the system and OS, you use the SDK and C++. Of course this gets more complicated because you then have to deal with two different systems, mac and windows. I believe this is the area where there should be more support to make it easier for creating sophisticated and dynamic applications. Just trying to set up the environment to start a project in the windows platform is a frustrating process of trial and error.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...