Tutorials

1.Introduction #

1.1.Creation of an Element #

This first script is basically the same as the one in ‘Quick Start’. It just creates an Element with a Block as its geometry. For a detailed description of this script please refer to the Quick Start page.

#===============================================================================
 #
 # CREATING AN ELEMENT
 #
 #===============================================================================

#-----------------------------
 # 1. Import Lexocad libraries.
 #-----------------------------

import OpenLxApp as lx

#----------------------------
 # 2. Get the active document.
 #----------------------------
 doc = lx.Application.getInstance().getActiveDocument()

#--------------------------------------
 # 3. Create a geometry in the document.
 #--------------------------------------
 block = lx.Block.createIn(doc)

#--------------------------------------------------------------------
 # 4. Create an element in the document and assign the geometry to it.
 #--------------------------------------------------------------------
 elem = lx.Element.createIn(doc)
 elem.setGeometry(block)

#----------------------------------
 # 5. Set the geometry's parameters.
 #----------------------------------
 block.setXLength(5)
 block.setYLength(5)
 block.setZLength(5)

#---------------------------
 # 6. Recompute the document.
 #---------------------------
 doc.recompute()

1.2.Translating an Element #

This script shows how to translate (move) an Element. Translations can be done in the local space of the Element (LCS – Local Coordinate System) or in the global space of the scene (WCS- World Coordinate System

import Geom
import OpenLxApp as lx
import OpenLxUI as ui

app = lx.Application.getInstance()
doc = app.getActiveDocument()

#--------------------
# Creating first Block.
#--------------------
block1 = lx.Block.createIn(doc)
elem1 = lx.Element.createIn(doc)
elem1.setGeometry(block1)

origin1 = Geom.Pnt(0, 0, 0)
zDir1 = Geom.Dir(0, 0, 1)
xDir1 = Geom.Dir(1, 1, 0) # This rotates the Block 45 deg. around the Z-Axis

axis1 = Geom.Ax2(origin1, zDir1, xDir1)
elem1.setLocalPlacement(axis1)

#---------------------
# Creating second Block.
#---------------------
block2 = lx.Block.createIn(doc)
elem2 = lx.Element.createIn(doc)
elem2.setGeometry(block2)

origin2 = Geom.Pnt(0, 0, 0)
zDir2 = Geom.Dir(0, 0, 1)
xDir2 = Geom.Dir(1, 0, 0)

axis2 = Geom.Ax2(origin2, zDir2, xDir2)
elem2.setLocalPlacement(axis2)

#-------------------------------------------------------------------------------
# An element can be translated from its actual position, according to a vector
# and its local coordinates system (LCS) or the world coordinate system (WCS).
#-------------------------------------------------------------------------------
x = 5; y = 0; z = 0;
translation = Geom.Vec(x, y, z) # Defining a translation of 5 along X-Axis.
elem1.translate(translation, Geom.CoordSpace_LCS) # Translating first Block (LCS)
elem2.translate(translation, Geom.CoordSpace_WCS) # Translating second Block (WCS)

#----------------------
# See the difference...
#----------------------
doc.recompute()

1.3.Rotating an Element #

This script shows how to rotate an Element around an axis. The rotation can be done in the local space of the Element (LCS) or the global space of the scene (WCS). The rotation axis is defined by the Geom.Ax1 class. All angles are in radians.

import Base, Geom
import OpenLxApp as lx
import OpenLxUI as ui

import math
app = lx.Application.getInstance()
doc = app.getActiveDocument()

#--------------------
# Creating first Block.
#--------------------
block1 = lx.Block.createIn(doc)
elem1 = lx.Element.createIn(doc)
elem1.setGeometry(block1)

origin1 = Geom.Pnt(5, 0, 0)
zDir1 = Geom.Dir(0, 0, 1)
xDir1 = Geom.Dir(1, 0, 0)

axis1 = Geom.Ax2(origin1, zDir1, xDir1)
elem1.setLocalPlacement(axis1)

red = 255; green = 0; blue = 0;
myColor = Base.Color(red, green, blue)
elem1.setDiffuseColor(myColor)

#---------------------
# Creating second Block.
#---------------------
block2 = lx.Block.createIn(doc)
elem2 = lx.Element.createIn(doc)
elem2.setGeometry(block2)

origin2 = Geom.Pnt(5, 0, 0)
zDir2 = Geom.Dir(0, 0, 1)
xDir2 = Geom.Dir(1, 0, 0)

axis2 = Geom.Ax2(origin2, zDir2, xDir2)
elem2.setLocalPlacement(axis2)

#-------------------------------------------------------------------------------
# An element can be rotated around an axis, according to its local coordinates 
# system (LCS) or the world coordinate system (WCS).
#-------------------------------------------------------------------------------
ang = math.radians(45)
rotationPoint = Geom.Pnt(0, 0, 0)

xAxis = Geom.Ax1(rotationPoint, Geom.Dir(1, 0, 0))
yAxis = Geom.Ax1(rotationPoint, Geom.Dir(0, 1, 0))
zAxis = Geom.Ax1(rotationPoint, Geom.Dir(0, 0, 1))

elem1.rotate(xAxis, ang, Geom.CoordSpace_LCS) # Rotating the first Block (LCS)
elem1.rotate(yAxis, ang, Geom.CoordSpace_LCS) #
elem1.rotate(zAxis, ang, Geom.CoordSpace_LCS) #

elem2.rotate(xAxis, ang, Geom.CoordSpace_WCS) # Rotating the second Block (WCS)
elem2.rotate(yAxis, ang, Geom.CoordSpace_WCS) #
elem2.rotate(zAxis, ang, Geom.CoordSpace_WCS) #

#----------------------
# See the difference...
#----------------------
doc.recompute()

1.4.Copying an Element #

This script shows how to copy an Element.

import Geom
import OpenLxApp as lx
import OpenLxUI as ui

app = lx.Application.getInstance()
doc = app.getActiveDocument()

#----------------------
# Creating 1st element.
#----------------------

cylinder = lx.RightCircularCylinder.createIn(doc)
elem1 = lx.Element.createIn(doc)
elem1.setGeometry(cylinder)

cylinder.setHeight(0.5)
cylinder.setRadius(0.5)

#-------------------------------------
# Copying and positioning 2nd element.
#-------------------------------------

elem2 = elem1.copy()
elem2.translate(Geom.Vec(0, 2, 0), Geom.CoordSpace_WCS)
doc.recompute()

1.5.Setting the color of an Element #

This script shows how to set the color, transparency and line width of an Element. There are four colors in Lexocad which define the visual appearance of an Element:

  • Diffuse Color
  • Emissive Color
  • Specular Color
  • Ambient Color

Please refer to the Draw.OglMaterial class for more information.

import Base
import OpenLxApp as lx
import OpenLxUI as ui

app = lx.Application.getInstance()
doc = app.getActiveDocument()

block = lx.Block.createIn(doc)
elem = lx.Element.createIn(doc)
elem.setGeometry(block)

#---------------------------------
# 5. Define the visual appearance.
#---------------------------------
red = 255; green = 0; blue = 0;
transparency = 50;
lineThickness = 5;

#----------------------------------
# 6. Set the values to the element.
#----------------------------------
elem.setDiffuseColor(Base.Color(red, green, blue))
elem.setTransparency(transparency)
elem.setLineWidth(lineThickness)

#--------------------------- 
# 7. Recompute the document.
#---------------------------
doc.recompute()

1.6.Removing an Element #

This script shows how to remove an Element from the document. This can be done by calling the document’s ‘removeObject()’ method. Please be aware that the Element still exists in memory, it just does not belong to the document anymore. You can reassign it by calling the ‘addObject()’ method. This can be useful for undo/redo operations.

import OpenLxApp as lx
import OpenLxUI as ui

app = lx.Application.getInstance()
doc = app.getActiveDocument()

#--------------------------------------
# First create the element.
#--------------------------------------
block = lx.Block.createIn(doc)
elem = lx.Element.createIn(doc)
elem.setGeometry(block)
doc.recompute()

# Do whatever you want with the element

#--------------------------- 
# Delete the element.
#---------------------------
doc.removeObject(elem)
doc.recompute()

2.Creating Geometries #

2.1.Creating a Block Geometry #

2.2.Creating a Cone Geometry #

2.3.Creating a Cylinder Geometry #

2.4.Creating a Tube Geometry #

2.5.Creating a Sphere Geometry #

2.6.Creating an Extrusion Geometry #

2.7.Creating a BRep Geometry #

Help Guide Powered by Documentor
Suggest Edit