this is a parser that mimics the behavior of IMOS IX 2023 linear division parser behavior it takes input linear division as a string "1:2" the total length and an optional divider thickness parameter the division is none-virtual and it returns an array of numbers representing lengths in mellimeter for each zone
there are two main types of input values: ratios and absolute
- ratios: 1:2 => ratio:ratio (the second division is double the first section)
- absolute 1mm:1 => absolute:ratio (the first section is 1mm of length and the rest is taken by the second)
- for x:y:z 1:2:1 && total length == 600mm
for normal ratio values substract absolute values from total length, then divide the rest by the total parts => each part gets an absolute value
- figuring out the value of 1 => 600mm / total parts => 600mm / 4 = 150mm == Ref
- Ref x X : Ref x Y : Ref x Z
- 150mm : 300mm : 150mm
- for A [ Y ] : 1 == 1 [ 100mm ] : 1 && total length == 480mm
should be treated like a normal ratio => getting an absolute value X = 480mm / 2 == 240mm
adding 2 constraints
- Result / [ Y : 100mm ] == Natural Number
- Result <= 240mm
- method: while (Result < 240mm) => { Result += 100mm} => Result == 200mm
- absolute value 200mm : 280mm
":" is implicit in the linear division definition
- for X{LinDiv}:Y == 3{1:2}:1
should first parse the Repetition to show explicit divisions
- multiply the division inside {} by X
- 1:2:1:2:1:2:1
- treat it like a normal ratio division
- this one is tricky as its position isn't within the zone size as for a -X mm:1:-Y mm for a total length of 1000mm the first negative value is positioned in -200mm and second is in 1200mm
for 30mm+1:n* ((X-60)/round((X-60)/100)) mm:30mm
X
is the total length and X-60 is the Rest from the substruction of 60mm == 30mm + 30mm
round(rest/100)
to get the step
- adding 0.5 to it => maximum is 100mm
- for these kind of values the absolute value is taken from the total length before proceeding calculations
for 1+30mm:1:1 for a total length of 462mm
- 462mm-30mm == 432mm
- 432mm/3 = 144mm
- for the first part we add 30mm which results in 144mm+30mm = 174mm
- absolute values: 174mm:144mm:144mm
- ratio + absolue mm the mm showing outside in a born makes the value absolute and the other operands without mm ratios could use (expression) mm as well
- x*y:1 is invalid as it doesn't allow * / operators outside parentheses
- x: (a * b)+c+(d * f mm) the intuitive behavior for this is to have (d * f mm) absolute (in mm) and other operands ratio BUT IT DOESN'T: if we have a mm inside () the entire section (a * b)+c+(d * f mm) becomes absolute
- function(x) + a : y => absolute : absolute you may consider function(x) like previous, a value that makes the entire section absolute if not in parentheses
- (function(x)) + a : y => ratio : ratio even if you do function(x)mm + y still => absolute + absolute you need to do (function(x))mm + y to have => absolute + ratio
- 1{x:y}:z => x:y:z the number occurences of x:y is defined by the left hand operand value
- x + 2{x:y}:1 => x+x:y:x:y:1 when we have an addition to a repetition on the left side it is added only to the first occurence of the inside division
- 2{x:y} + x:1 => x:y:x:y+x:1
- (expression){x:y} the result of expression left hand to repetition is rounded if inside () (2.7){1mm}:1 => 3{1mm}:1 if not inside () and the value is not integer it results in a weird behavior 2.4{1mm}:2 => 2.1:1:1:1:2 => the number of repetitions is 4 and it makes the first division a fractional part of 2
- n * y cannot be multiplied to a grouping that contains mm n*(100mm) is wrong even if mm outside (100mm+1)mm
- n* y cannot belong to a binary expression like n* y + 1 (multiple divisions with n* is not supported yet you could use only one)
$ npm install imos-linear-division
Once the package is installed, you can import the library using import or require approach:
import { processLindiv } from 'imos-linear-division';
the processLinDiv function returns an array of numbers (in mm) takes a string linear division the total length and an optional dividerthickness
const result = processLindiv("50mm+2:2:2{50mm}:3", 500, 20)
Example of usage with variables
processLindiv("$var1 mm+$var2:$var3:$var3{$var1 mm}:$var4", 500, 20, { var1: 50, var2: 2, var3: 2, var4: 3 })
// result == [127.14, 77.14, 50, 50, 115.71]
Example of usage with variables
processLindiv("$var1 mm+$var2:$var3:$var3{$var1 mm}:$var4", 500, 20, { var1: 50, var2: 2, var3: 2, var4: 3 })
// result == [127.14, 77.14, 50, 50, 115.71]