Where can I find script samples to start with?
Visit the Cyclone 3DR GitHub page
You can visit Cyclone 3DR Scripts to have access to a list of scripts samples.
For example, have a look at MyFirstScript, it will help you learn how to get started on scripting with Cyclone 3DR.
Basic sample: Inspection Cloud vs Mesh
Additionally, here is a small sample that creates a mesh from a selected point cloud (using SPoly.Direct3DMesh) and then performs an inspection analysis (using SPoly.Compare)
function ErrorMessage(iMessage)
{
throw new Error(iMessage);
}
if(selCloudTbl.length == 0)
{
ErrorMessage("Please select a point cloud.");
}
var iCloud = selCloudTbl[0];
var deviationError = 0;
var miniAverageDist = 0.05;
var sizeHoles = miniAverageDist*3;
var iResult =
SPoly.
Direct3DMesh(iCloud,deviationError,miniAverageDist,meshHoles,sizeHoles);
if (iResult.ErrorCode ==0)
{
var newMesh = iResult.Poly;
iCloud.SetVisibility(false);
newMesh.AddToDoc();
print(
"The mesh is created.\n");
}
else
{
ErrorMessage("An error occurred. No mesh was created.");
}
var maxDist = 1;
var iInspection = newMesh.Compare( iCloud, maxDist, 1, true, 0, 90, true);
if (iInspection.ErrorCode == 1)
{
ErrorMessage("An error occurred during the inspection.");
}
else
{
var inspectedMesh = iInspection.Poly;
inspectedMesh.AddToDoc();
inspectedMesh.SetName("Inspected mesh");
newMesh.SetVisibility(false);
print(
"Inspection succeeded.\n");
}
Provide point cloud edition and creation methods.
Definition: Reshaper.h:3306
static Array< SCloud > FromSel()
Get all the selected SCloud.
Provide triangular mesh edition and creation methods.
Definition: Reshaper.h:6993
@ ALL_CLOSED
Try to close the SPoly (watertight mesh). As the word "try" suggests, there is no warranty that the m...
Definition: Reshaper.h:7074
static Object Direct3DMesh(SCloud cloudToMesh, number deviationError, number minAverageDist, HoleOptionEnum optionHole, number sizeTriHole, boolean isScanDirIgnored=false, number iSmoothingDistanceFactor=0)
Make a direct 3D mesh of a SCloud according to different strategies (regular, deviation,...
print(any arg)
Print to output the argument.
How can I retrieve an element from the current document?
There are 4 ways to request elements from the current document:
- All: To retrieve all elements from a given type.
- FromSel: To retrieve the current selected elements.
- FromName: To query elements from their names.
- FromClick: To query an element clicked by the user.
These functions are static and accessible in SComp or any derived SComp class (such as SPoly.All for example). The usage will depend on the type of object you want to query.
- All()
To find all elements of a given type.
The function will return a table with all elements of a defined type depending on a visibility criterion.
static Array< SCloud > All(VisibilityEnum visCrit=SComp.ANY_VISIBILITY)
Get all the SCloud in the document.
Provide point edition and creation methods.
Definition: Reshaper.h:6761
static Array< SPoint > All(VisibilityEnum visCrit=SComp.ANY_VISIBILITY)
Get all the SPoint in the document.
static Array< SPoly > All(VisibilityEnum visCrit=SComp.ANY_VISIBILITY)
Get all the SPoly in the document.
- FromSel()
To find all selected elements of a defined type.
Selection can be used before running script or during script execution.
The function will return a table with all selected elements of a defined type.
And more, selection can be done during SDialog execution.
if(tblPlane.length > 0)
var firstPlane = tblPlane[0];
Provide plane edition and creation methods.
Definition: Reshaper.h:11349
static Array< SPlane > FromSel()
Get all the selected SPlane.
- FromName()
To find an element of a defined type by its name.
The function will return a table with all elements of a defined type with the given name.
static Array< SCloud > FromName(string name)
Search all the SCloud with the given name.
static Array< SPoint > FromName(string name)
Search all the SPoint with the given name.
- FromClick()
Unlike previous function, this command will pause the script and wait until you click a point in the document.
If the clicked point corresponds to an element of the defined type, the element is returned, else an error is returned.
A specific case exists for SPoint function which can return the clicked entity or a projection of the clicked point.
if (result.ErrorCode != 0)
{
print("An error occurred. No cloud has been selected.");
}
else
{
print("Point Cloud " + result.Cloud.GetName() + " has been selected for meshing.");
}
static Object FromClick()
Launch an interaction to select a SCloud in the scene.
How can I insert elements in my document?
By default, imported objects are not inserted inside the current document.
Use SComp.AddToDoc() to insert an element in the current document.
How can I import files?
- FromFile()
This static function is available for most classes. For example, SPoly.FromFile, SCloud.FromFile, etc.
The function will return elements loaded, it means that the elements are not inserted in the current document.
if (load.ErrorCode == 0)
{
var mesh = load.PolyTbl[0];
print("The import of the object " + mesh.GetName() + " succeeded.");
mesh.AddToDoc();
}
else
{
throw new Error("The import of the object failed.");
}
static Object FromFile(string fileName)
Import a set of meshes from a file and create the associated SPoly objects.
- OpenDoc()
To open a .3dr file.
All the objects are directly inserted in the current document.
var res = OpenDoc( 'C:/MyFile.3dr' );
if (res.ErrorCode == 0)
{
print("\nThe file has been correctly loaded.");
}
else
{
throw new Error("An error occurred during the loading.");
}
- SSurveyingFormat.ImportProject()
This function will import dxf, LandXml and E57 files.
Elements are not added in the current document.
if(res.ErrorCode != 0)
throw new Error("An error occurred during the import.");
for(i = 0; i < res.CloudTbl.length; i++)
res.CloudTbl[i].AddToDoc();
How can I export files?
- Save()
This static function is available for most of classes. For example, SPoly.Save, SMultiline.Save and SCloud.Save
Elements are not added in the current document.
var filePath = "C://" + mesh.GetName()+".obj";
var result = mesh.Save(filePath,false,matrix);
if (result.ErrorCode == 0)
{
print("The export of the object " + mesh.GetName() + " succeeded.");
}
else
{
throw new Error("The export of the object " + mesh.GetName() + " failed.");
}
Provide matrix with 3 lines and 4 columns to make any transformation.
Definition: Reshaper.h:2019
static SMatrix FromActiveCS()
Return the matrix associated to the active coordinate system.
static Array< SPoly > FromSel()
Get all the selected SPoly.
- SSurveyingFormat.ExportProject()
This function will export all input elements in a DXF file.
How can I handle errors in my code?
Instead of using throw new Error, you can create your function, based on the example below, to show a dialog with your error message.
function ErrorMessage(iMessage)
{
myDlg.
AddLine(iMessage,
false, Array(), 1);
myDlg.Execute();
throw new Error(iMessage);
}
if (result.ErrorCode != 0)
{
ErrorMessage("An error occured. No cloud has been selected.");
}
else
{
print(
"Point Cloud " + result.Cloud.GetName() +
" has been selected for meshing.");
}
Provide simple dialog creation methods.
Definition: Reshaper.h:1381
AddLine(string label, boolean isInput, Object labParams={}, string defaultInput="")
Adds a new line to the Dialog.
static SDialog New(string title="Dialog")
Dialog constructor.
How to create new objects?
In script, you can create new object through the use of static methods nammed New.
Depending on the object, several constructors may exist with different input parameters.
static SPoint New()
Default constructor to create an empty new SPoint. X = Y = Z = 0.
Once your class object has been created, you can call any available method from it, like for instance:
AddToDoc()
Add the object to the document.
How to copy objects?
To copy a script object, you cannot use the = operator, indeed the = operator merely copies the object reference and does not create a copy.
To copy a SObject instance, you must use the New( objectToCopy ) constructor available for most classes.
For instance:
print(myPoint.GetX());
var samePoint = myPoint;
samePoint.SetX(32);
print(myPoint.GetX());
print(samePoint.GetX());
print(myPoint.GetX());
print(pointCopy.GetX());
SetX(number x)
Set the X coordinate.