Tips and tricks for Ubuntu server

This topic will contain various tips and tricks that can be used on a Linux server; especially Ubuntu. The tips themselves will appear as comments to this topic

Rebol is a powerful, terse language. It is free. It has two main versions (there are more, but let me talk only about two) One is Rebol/core and the other is Rebol/view Both are totally free

Rebol/view works in a GUI environment. But that is the only one which has got advanced functions such as reading over https etc. Unfortunately on an Ubuntu server, Rebol/view does not work as it does not have X-Windows.

No worries; do the following.

Rebol/core and Rebol/view both can be downloaded for free from http://www.rebol.com/downloads.html Choose the one that is appropriate for your serve CPU (ARM? x86? )

Install a substitute for X-Windows as explained here: https://stackoverflow.com/a/24496293/1648362
Note the comment there – you need to install a font too

Use the following script after the prerequisite are installed

#! /usr/bin/bash

pkill Xvfb
xvfb-run rebol-view --noinstall --noviewtop $1 $2 $3

FreeCAD is a powerful CAD software with lots of scripting capabilities.

On Ubuntu 19.04 freecad can only be installed via Snap (at least on Hetzner servers)

snap install freecad

Now snap installs freecad in some of its own managed folders usually /snap/… so not sure how to programmatically use it inside Python itself;

But you can run freecad without the GUI as follows

freecad -c <SCRIPTNAME>

More documentation here: https://wiki.freecadweb.org/Start_up_and_Configuration

Here is a nice wrapper for using Freecad on the web. But it needs access to the lib of freecad and when Snap is used to install Freecad, there is a confusion on where exactly would be this lib

https://github.com/danielfalck/freecadweb

Discussions about that is here:

https://forum.freecadweb.org/viewtopic.php?t=6470

Here is a tiny python script that can be run from the command line at an Ubuntu server; with Freecad installed via snap. It produces a file called test.FCStd in the folder where this script is called. The last line of the script should be exit() else the program will not quit

import Part
#doc = App.activeDocument()
doc = FreeCAD.newDocument()
fnm = './test.FCStd'
c = Part.Circle() 
c.Radius=10.0  
# create a document with a circle feature
f = doc.addObject("Part::Feature", "Circle")  
f.Shape = c.toShape() # Assign the circle shape to the shape property 
doc.recompute()
doc.saveCopy(fnm)
exit()

This is another script for making 2D drawing

import Part, FreeCAD, Draft
#doc = App.activeDocument()
doc = FreeCAD.newDocument()
fnm = './test2.FCStd'
p1 = FreeCAD.Vector(0, 0, 0)
p2 = FreeCAD.Vector(1500, 2000, 0)
p3 = FreeCAD.Vector(4000, 0, 0)
Wire = Draft.makeWire([p1, p2, p3])
vector = FreeCAD.Vector(0, 500, 0)
Offset_1 = Draft.offset(Wire, vector, copy=True)
Offset_2 = Draft.offset(Wire, 3*vector, copy=True)
Offset_3 = Draft.offset(Wire, 6*vector, copy=True)
Offset_4 = Draft.offset(Wire, 9*vector, copy=True)
Offset_5 = Draft.offset(Wire, 1.5*vector, copy=True, occ=True)
doc.recompute()
doc.saveCopy(fnm)
exit()

Here is someone who has created a Freecad viewer on the web. It has its own Python based server running on port 8888 and that server allows for files to be uploaded to it; which is then processed by the command line Freecad… It is still work in progress, but looks promising https://github.com/danielfalck/freecadweb

Using ezdxf and CSG libraries in Python to create a CSG output

import ezdxf
from ezdxf.render.forms import cube, cylinder_2p
from ezdxf.addons.pycsg import CSG

# create new DXF document
doc = ezdxf.new()
msp = doc.modelspace()

# create same geometric primitives as MeshTransformer() objects
cube1 = cube()
cylinder1 = cylinder_2p(count=32, base_center=(0, -1, 0), top_center=(0, 1, 0), radius=.25)

# build solid union
union = CSG(cube1) + CSG(cylinder1)

# convert to mesh and render mesh to modelspace
union.mesh().render(msp, dxfattribs={'color': 1})
# build solid difference
difference = CSG(cube1) - CSG(cylinder1)
# convert to mesh, translate mesh and render mesh to modelspace
difference.mesh().translate(1.5).render(msp, dxfattribs={'color': 3})

# build solid intersection
intersection = CSG(cube1) * CSG(cylinder1)

# convert to mesh, translate mesh and render mesh to modelspace
intersection.mesh().translate(2.75).render(msp, dxfattribs={'color': 5})

doc.saveas('csg.dxf')

The converted csg.dxf file has all the proper output

Here is a pure ezdxf python script to create a 2D DXF file

import ezdxf

# Create a new DXF document.
doc = ezdxf.new(dxfversion='R2010')

# Create new table entries (layers, linetypes, text styles, ...).
doc.layers.new('TEXTLAYER', dxfattribs={'color': 2})

# DXF entities (LINE, TEXT, ...) reside in a layout (modelspace, 
# paperspace layout or block definition).  

msp = doc.modelspace()

# Add entities to a layout by factory methods: layout.add_...() 
msp.add_line((0, 0), (10, 0), dxfattribs={'color': 7})
msp.add_text(
    'Test', 
    dxfattribs={
        'layer': 'TEXTLAYER'
    }).set_pos((0, 0.2), align='CENTER')

# Save DXF document.

doc.saveas('test.dxf')

And presto! You get a DXF file, where the entities are in the model space

Sometimes a PHP program needs to execute something on the server (the execution time could be large, or unknown) but the user’s experience should not be troubled.

Here is the answer: