←  Technical Questions

nanoCAD forum

»

Script example

Gintaras's Photo Gintaras 27 Mar 2012

Hi,

Can someone give Java or Vb script example? And how to register script to nanocad?
Quote

ISL's Photo ISL 27 Mar 2012

Hello Gintaras,

JS and VBS scripts can be run from within nanoCAD by JS and VBS commands.

Simple examples:

VBScript
' Create a Ray object in model space
Dim rayObj
Set rayObj = ThisDrawing.ModelSpace.AddRay("3,3,0", "1,3,0")

' Create a line object in model space
Dim lineObj
Set lineObj = ThisDrawing.ModelSpace.AddLine("0,0,0", "2,2,0")

' Create a circle object in model space
Dim circObj
Set circObj = ThisDrawing.ModelSpace.AddCircle("20,20,0", 3)

' Create an ellipse object in model space
Dim ellObj
Set ellObj = ThisDrawing.ModelSpace.AddEllipse("5,5,0", "10,20,0", 0.3)
msgbox("zoom all")

' ZoomAll
ThisDrawing.Utility.Prompt "Perform a ZoomAll"
ThisDrawing.Application.ZoomAll


JScript
var ent = new Array
var pt = new Array
try {
ThisDrawing.Utility.GetEntity(ent,pt,"Select an ent1")
ThisDrawing.Utility.Prompt(ent[0].EntityName + " at " + pt_toString(pt[0]));
}
catch (ex) {
ThisDrawing.Utility.Prompt("oops");
}

function pt_toString(pt)
{
var sp = new VBArray(ThisDrawing.Utility.CreateSafeArrayFromVector(pt))
return sp.toArray().toString();
}

More information on nanoCAD API can be found in the nanoCAD Developers' Club http://developer.nanocad.com.
Quote

Gintaras's Photo Gintaras 27 Mar 2012

I try these two scripts, but nothing happened....
Quote

Gintaras's Photo Gintaras 27 Mar 2012

Hi,

Why Java and Vb scripts don`t work on english version of nanoCAD?
Quote

ISL's Photo ISL 27 Mar 2012

JScripts and VBScripts definetely work in English version of nanoCAD.


It's very strange to hear that nothing happens, scripts can either work or end with an error.

Please provide us with a step by step report of your actions and a screenshot of your command line.
Quote

cean's Photo cean 04 Apr 2012

cool. I like this. vbs works for me.
Quote

global_cadserver@yahoo.com's Photo global_cadserver@yahoo.com 20 Apr 2012

I just installed the nanocad and run in demo mode. run the vbscript and javascripts from the above samples. nothing happen
Quote

ISL's Photo ISL 23 Apr 2012

I've attached the scripts to avod copy/paste problems. JS and VBS commands require scripts to be in ANSI encoding, Unicode is not supported yet.

nanoCAD Eng 3.5 3.5.1880.1071, © 2012 Nanosoft

Command: filedia
FILEDIA <1>: 0

Command: vbs
Script file path: c:\MyScripts\zoom.vbs
Perform a ZoomAll

Command: js
Script file path: c:\MyScripts\GetEnt.js
1 found
AcDbEllipse at 8.217525248701036,21.901834795531684,0

Command: filedia
FILEDIA <0>: 1

Attached Files

Quote

joseluis's Photo joseluis 22 Mar 2013

HI. I am new in nanocad. Thanks for the examples.

The vbs works for me OK in the Model tab (not in Layout).
The js requests info in the console. I don´t know what values should I enter for ent, I used values such 1 or 10 and the js runs into the catch. I tried also indexes of created entities. This is the console outputs.


Select an ent or [?/]: 10
Specify opposite corner: 20
0 found
oops
Quote

ISL's Photo ISL 25 Mar 2013

Hi joseluis,

Please add a viewport to the layout where you are running zoom.vbs (switch to the layout, View->Viewports->1 Viewport), and zoom will work as designed.

Regarding GetEnt.js, please pick an entity instead of entering number:

Select an ent or [?/]: <pick the ellipse created by zoom.vbs>
1 found
AcDbEllipse at 5.550731437335969,17.826057121504846,0
Quote

bllinky's Photo bllinky 13 Jul 2014

Hello
Me need vbs to insert block. Pleas help!
Quote