Importing external libraries using include or use in Openscad

Source: Internet
Author: User
Tags openscad

First. OPENSCAD using include or use to import external libraries


English version of this article reference: Https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Include_Statement



=============================================



Openscad can quickly introduce external libraries, which are very powerful. This means that not only can some utility functions be encapsulated, put into the library for reuse, but also easily share code and use third-party code libraries. The code that contains the external library can be used with the include and use two statements:


    • include <filename>All content is written in the same way as the main file.

    • use <filename>Modules and functions are introduced, but no other definitions are executed.

Second, OPENSCAD library file path


The library file is placed by default in the design file directory or the OPENSCAD installation directory, either by using relative directories or by specifying the absolute path directly. The new version can be used with a defined user-Library path to view Openscad_user_manual/libraries, which describes several library files that are contained in OPENSCAD.

Note: Windows and Linux/mac use a different path delimiter. Windows uses \, for example, Directory\file.ext, other uses /, such as Directory/file.ext. This leads to some cross-platform issues, but OPENSCAD is handled correctly on Windows /, so include or use in all files/can work on all platforms .



Third. using the Include method to include a file


These include <filename> default values can be overridden by the main file's code by using the Allow default variables to be specified in the library. The OPENSCAD variable can only be a value during run time, and when multiple assignments are given, the last value is applied, and the variable is assigned the first time it is created. When assigning values in a library, subsequent changes to the default value of variables must be assigned before the include, referring to the second example below.





Create a ring library file (define a function and provide an example):





RING.SCAD:


Module ring (R1, R2, h) {difference () {cylinder (R = r1, h = h);     Translate ([0, 0,-1]) cylinder (r = r2, h = h+2); }} ring (5, 4, 10);

 


Includ the library:



Include <ring.scad>; Rotate ([0, 0]) ring (10, 1, 1);
Iv. introduction of functions and modules using the Use method


Show only the rotated rings, using the use method:



Use <ring.scad>; Rotate ([0, 0]) ring (10, 1, 1);


If using the use method, make sure to place the usage statement at the beginning of the file, at least not in the module!



The following code can work:



A.scad Use <ring.scad>;  Module A () {ring (); }


The following usage will cause a syntax error:



A.SCAD Module A () {use <ring.scad>;  Ring (); }


The include default variables in the can be overridden, for example:



Lib.scad



I=1; k=3;     Module X () {echo ("Hello World"); Echo ("i=", I, "j=", J, "k=", K); }

Hello.scad



j=4; Include <lib.scad>; X (); i=5; X (); K=j; X ();


The resulting output is as follows:



echo: "Hello World" Echoes: "I=", 5, "j=", 4, "k=", 4 echo: "Hello World" Echo: "I=", 5, "j=", 4, "k=", 4 echo: "Hello World" "ECHO:" i= ", 5," j= ", 4," k= ", 4


However, if you put j=4; after include, it will fail, and the output will be as follows:


echo: "Hello World" Echoes: "I=", 5, "j=", 4, "k=", undef echo: "Hello World" Echo: "I=", 5, "j=", 4, "k=", undef ECHO: "Hel Lo World "ECHO:" i= ", 5," j= ", 4," k= ", undef





Importing external libraries using include or use in Openscad


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.