Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/scad_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::string::*;
use scad_type::*;
extern crate nalgebra as na;

use std::rc::Rc;
use std::vec::Vec;


Expand Down Expand Up @@ -225,6 +226,35 @@ pub enum ScadElement {

Color(na::Vector3<f32>),
NamedColor(String),

/// Call to user defined / included scad function.
FunctionCall(String, Option<Rc<FnCallArgs>>, Option<Rc<FnCallNamedArgs>>),
}

/// Direct function call arguments - vector of values.
pub type FnCallArgs = Vec<Box<dyn ScadType>>;

/// Named arguments - vector of tuples - name, argument value
pub type FnCallNamedArgs = Vec<(String, Box<dyn ScadType>)>;

/// OpenSCAD Function call arguments, "arg_value, ..." comma separated printout.
impl ScadType for FnCallArgs {
fn get_code(&self) -> String {
let arg_strs: Vec<String> = self.iter().map(|arg| arg.as_ref().get_code()).collect();
arg_strs.join(", ")
}
}

/// OpenSCAD Function call named arguments, "arg_name = arg_value", ... comma separated printout.
impl ScadType for FnCallNamedArgs {
fn get_code(&self) -> String {
let named_strs: Vec<String> = self.iter().map(|arg| {
let arg_name = &arg.0;
let arg_value = arg.1.as_ref();
format!("{} = {}", arg_name, arg_value.get_code())
}).collect();
named_strs.join(", ")
}
}

impl ScadElement
Expand Down Expand Up @@ -353,6 +383,24 @@ impl ScadElement
ScadElement::Hull => String::from("hull()"),
ScadElement::Minkowski => String::from("minkowski()"),
ScadElement::Intersection => String::from("intersection()"),

ScadElement::FunctionCall(fn_name, fn_args, fn_named_args) => {
let mut all_args_str = String::new();
// direct arguments (if any) come first
if let Some(ref args) = fn_args {
all_args_str += &args.get_code();
}
// followed by named arguments (if any)
if let Some(ref named_args) = fn_named_args {
let named_str = named_args.get_code();
// add separator comma if both direct and named args are present
if !all_args_str.is_empty() && !named_str.is_empty() {
all_args_str += ", ";
}
all_args_str += &named_str;
}
format!("{}({})", fn_name, all_args_str)
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/scad_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ pub struct ScadFile
{
objects: Vec<ScadObject>,

/// List of .scad scripts to be included.
/// Current implementation does not allow to use include statement anywhere in file as it should.
/// (for own variable pre-setting)
includes: Vec<String>,

detail: i32,
}

Expand All @@ -24,6 +29,8 @@ impl ScadFile
ScadFile {
objects: Vec::new(),

includes: Vec::new(),

detail: 0,
}
}
Expand All @@ -41,6 +48,11 @@ impl ScadFile
result = result + "$fn=" + &self.detail.to_string() + ";\n";
}

for include in &self.includes
{
result = result + &format!("include <{}>", include) + "\n";
}

for object in &self.objects
{
result = result + &object.get_code() + "\n";
Expand All @@ -49,6 +61,11 @@ impl ScadFile
result
}

pub fn add_include(&mut self, include: String)
{
self.includes.push(include);
}

pub fn add_object(&mut self, object: ScadObject)
{
self.objects.push(object);
Expand Down
8 changes: 8 additions & 0 deletions src/scad_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ impl ScadType for na::Vector2<f32>
}
}

impl ScadType for na::Vector1<f32>
{
fn get_code(&self) -> String
{
String::from("[") + &self.x.get_code() + "]"
}
}

impl ScadType for f32
{
fn get_code(&self) -> String
Expand Down