Parametric design in MoI?

 From:  mkdm
7713.341 
Hi everyone!

here's the new code of my "PatternSelArray" and "ArrayReplicator".

The attached pictures show a simple example.

If you find bugs and errors, please, let me know...

Nice evening to all,

Marco (mkdm)

code:
// PatternSelectArray
function PatternSelArray()
{	
	this.size = [160,70];
	this.minsize = [160,70];
	
	this.addInput("In","pointarray");
	this.addInput("Start_Idx","number");
	this.addInput("Qt_Max","number");
	this.addInput("Step","number");		
	
	this.addOutput("Out","pointarray");
	this.addOutput("Elems","number");
	this.addOutput("Source","pointarray");
	
	this.properties = { mode:["Pattern + Subset", "Pattern + Subset", "Subset + Pattern"],
		pattern:"",
		start_idx:0,
		qt_max:-1,
		step:1 };
	
}

PatternSelArray.title = "PatternSelArray";
PatternSelArray.desc = "PatternSelArray";

PatternSelArray.prototype.onExecute = function()
{		
	/*
		source = the source array
		start_idx = index of the first element to pick
		numMaxElem = max number of elements that will be present in the resulting array
		step = step amount for the for..loop
	*/
	var getPointArraySection = function(source, start_idx, numMaxElem, step) {

		var result = new pointArray();
		
		if (source) {
			var sourceLength = source.getLength();
			for (i = start_idx, cont = 0; i < sourceLength && cont < numMaxElem; i += step, cont++) {
				var source_elem = source.getElement(i);
				
				result.push(source_elem.pt.x,
					source_elem.pt.y,
					source_elem.pt.z,
					source_elem.AngleX,
					source_elem.AngleY,
					source_elem.AngleZ,
					source_elem.Size);
			}
		}
		return result;
	}

	/*
		source = the source array
		pattern = string that define the selection pattern
			1 or + = pick element
			0 or - = skip element
	*/	
	var applyPatternToPointArray = function(source, pattern)
	{
		function ResultObj(pattern) {
			this.array = new pointArray();
			this.pattern = pattern;
		}

		var usePattern = false;
		var isMixedPattern = false;			
		var result = new ResultObj(pattern);
		
		if (pattern) {
			result.pattern = pattern.trim();
			
			// check the pattern
			if (result.pattern != '') {
				// check if pattern contains valid characters
				usePattern = result.pattern.match('^[+\-0-1]+$');
				
				if (usePattern) {
					// check if pattern contains only include characters (+ or 1)
					usePattern = !result.pattern.match('^[+1]+$');
					
					if (usePattern) {
						// if pattern contains both include and exclude characters, then the pattrn will be used
						// otherwise it means that contains only exclude characters (- or 0), and therefore
						// the output pointarray list will be empty
						isMixedPattern = !result.pattern.match('^[\-0]+$');
					}
				}
			}
		}

		if (usePattern) {
			if (isMixedPattern) {
				var len = source.getLength();
				var patLen = result.pattern.length;
				var cn = 0;
				
				for (i = 0; i < len ; i++) {
					if ( result.pattern[cn] === '1' || result.pattern[cn] === '+') {
								
						var source_elem = source.getElement(i);
						
						result.array.push(source_elem.pt.x,
							source_elem.pt.y,
							source_elem.pt.z,
							source_elem.AngleX,
							source_elem.AngleY,
							source_elem.AngleZ,
							source_elem.Size);
					}
					cn = (cn < (patLen - 1)) ? (cn + 1) : 0;
				}
			}
		} else {
			// elaborate the whole source pointarray
			result.pattern = '';
			result.array = source;		
		}

		return result;
	}

	// Business logic
	var source = this.getInputData(0, new pointArray());
	
	this.properties.start_idx = this.getInputData(1, this.properties.start_idx);
	this.properties.qt_max = this.getInputData(2, this.properties.qt_max);
	this.properties.step = this.getInputData(3, this.properties.step);

	if (this.properties.step <= 0) this.properties.step = 1;
	
	var patternedArray;
	var outputIntermediate;
	var outIntermLength;
	
	if (this.properties.mode[0] == "Pattern + Subset") {
		patternedArray = applyPatternToPointArray(source, this.properties.pattern);		
		outputIntermediate = patternedArray.array;		
		outIntermLength = outputIntermediate.getLength();
	} else {
		outputIntermediate = source;
		outIntermLength = source.getLength();
	}

	if ((this.properties.start_idx < 0) || (this.properties.start_idx > (outIntermLength - 1))) this.properties.start_idx = 0;
	
	if (this.properties.qt_max < 0) this.properties.qt_max = outIntermLength;
	
	outputIntermediate = getPointArraySection(outputIntermediate,
						this.properties.start_idx,
						this.properties.qt_max,
						this.properties.step);

	if (this.properties.mode[0] == "Subset + Pattern") {
		patternedArray = applyPatternToPointArray(outputIntermediate, this.properties.pattern);
		
		outputIntermediate = patternedArray.array;
		
		outIntermLength = outputIntermediate.getLength();
	}
						
	this.properties.pattern = patternedArray.pattern;
	patternedArray = null;
	
	this.outputs[0].label = "Out (" + outputIntermediate.getLength() + ")";
	this.outputs[1].label = "Elems (" + outputIntermediate.getLength() + ")";
	this.inputs[0].label = "In (" + source.getLength() + ")";
	
	this.setOutputData(0, outputIntermediate);
	this.setOutputData(1, outputIntermediate.getLength());
	this.setOutputData(2, source);
	
}

LiteGraph.registerNodeType("Arrays/PatternSelArray", PatternSelArray);

// ArrayReplicator
function ArrayReplicator()
{
	this.labels = "abcdef";
	this.size = [140,40];
	this.minsize = [140,40];

	this.internal = {a:1, b:1};
	
	this.addInput("In","pointarray");
	
}

ArrayReplicator.title = "Array Replicator";
ArrayReplicator.desc = "Array Replicator";

ArrayReplicator.prototype.onGetOutputs = function()
{
	var list = [];
	for ( var i = 0; i<this.labels.length; i++ )
	{
		var out = this.labels.charAt(i);
		if ( !(out in this.internal)) { list.push([this.labels.charAt(i),"pointarray", {locked:true}]); break; }
	}
	return list;
}

ArrayReplicator.prototype.onOutputAdded = function(out)
{
	this.internal[out.name] = 1;
	for ( var i in this.outputs ) this.outputs[i].locked = true;
	if (i>1) this.outputs[i].locked = false;
}

ArrayReplicator.prototype.onOutputRemoved = function(slot, name)
{
	if ( this.internal[name] ) delete this.internal[name];
	for ( var i in this.outputs ) this.outputs[i].locked = true;
	if ( this.outputs.length > 2 )this.outputs[i].locked = false;
}

ArrayReplicator.prototype.onAdded = function()
{
	for ( var i = 0; i<this.labels.length; i++ ) if ( this.internal[this.labels.charAt(i)] && this.outputs.length == i ) this.addOutput(this.labels.charAt(i), "pointarray");
	this.setDirtyCanvas(true);
}

ArrayReplicator.prototype.onExecute = function()
{
	var source = this.getInputData(0, new pointArray());

	this.inputs[0].label = "In (" + source.getLength() + ")";
	
	for ( var i = 0; i<this.outputs.length; i++)
	{
		this.setOutputData(i, source);
	}	
}

LiteGraph.registerNodeType("Arrays/ArrayReplicator", ArrayReplicator);
Image Attachments:
Size: 155.3 KB, Downloaded: 89 times, Dimensions: 1522x822px
Size: 1.1 MB, Downloaded: 113 times, Dimensions: 1918x1035px