def call(self, model, sprocket_list, continuous = interp.lcad_t): # Keywords continuous = True if interp.isTrue(continuous) else False # Get list of sprockets. if (len(sprocket_list) < 2): raise NumberSprocketsException(len(sprocket_list)) # Create sprockets. chain = belt.Belt(continuous) for sprocket in sprocket_list: if not isinstance(sprocket, list): raise lcadExceptions.WrongTypeException("list", type(sprocket)) if (len(sprocket) != 4): raise SprocketException(len(sprocket)) for elt in sprocket: if not isinstance(elt, numbers.Number): raise lcadExceptions.WrongTypeException("number", type(elt)) chain.addSprocket(belt.Sprocket([sprocket[0], sprocket[1], 0], [0, 0, 1], sprocket[2], sprocket[3])) chain.finalize() # Return chain function. return curveFunctions.CurveFunction(chain, "user created chain function.")
def call(self, model, tlist, *indices): # List. if isinstance(tlist, list): if (len(indices) != 1): raise lce.LCadException("Attempt to index list with multiple indices.") index = indices[0] if (index >= 0) and (index < len(tlist)): return ArefSymbol(tlist, index) else: raise lce.OutOfRangeException(len(tlist) - 1, index) # Numpy array. elif isinstance(tlist, numpy.ndarray): shape = tlist.shape if (len(indices) != len(shape)): raise lce.LCadException("Number of indices (" + str(len(indices)) + ") does not match array shape (" + str(len(shape)) + ").") for i in range(len(shape)): if (indices[i] < 0) or (indices[i] >= shape[i]): raise lce.LCadException(" index " + str(indices) + " is outside of " + str(shape)) return ArefSymbol(tlist, indices) else: raise lce.WrongTypeException("list, vector, matrix", functions.typeToString(type(tlist)))
def ldrawCoeffsToMatrix(model, coeffs): m = numpy.identity(4).view(lcadTypes.LCadMatrix) for x in mapping: val = interp.getv(interp.interpret(model, coeffs[x[0]])) if not isinstance(val, numbers.Number): raise lce.WrongTypeException("number", type(val)) m[x[1]] = val return m
def parseArgs(val): """ This is used by most of the geometry and part functions to parse list or vectors. The list / vector is truncated to 3 elements, as this is what they all use. """ # Numpy array. if isinstance(val, lcadTypes.LCadVector): return val.tolist()[0:3] # LCad list. if isinstance(val, list): if (len(val) < 3): raise lce.LCadException("Expected a list with 3+ members, got " + str(len(val))) for elt in val: if not isinstance(elt, numbers.Number): raise lce.WrongTypeException("number", type(elt)) return val[0:3] raise lce.WrongTypeException("list, vector", type(val))
def listToMatrix(a_list): if (len(a_list) == 12): m = numpy.identity(4).view(lcadTypes.LCadMatrix) for i in range(len(a_list)): if not isinstance(a_list[i], numbers.Number): raise lce.WrongTypeException("number", type(a_list[i])) m[mapping[i][1]] = a_list[i] return m if (len(a_list) == 6): return numpy.dot(translationMatrix(*a_list[:3]), rotationMatrix(*a_list[3:])).view( lcadTypes.LCadMatrix) if (len(a_list) == 4): vecs = [] for elt in a_list: if isinstance(elt, lcadTypes.LCadVector): vecs.append(elt[:3].copy()) continue if isinstance(elt, list): if (len(elt) != 3): raise lce.LCadException( "Expected a list with 3 members, got " + str(len(elt))) vecs.append(numpy.array(elt[:3])) continue raise lce.WrongTypeException("list, vector", interp.typeToString(type(elt))) for i in range(3): vecs[i + 1] = vecs[i + 1] / numpy.linalg.norm(vecs[i + 1]) return vectorsToMatrix(*vecs) raise lce.LCadException("Expected a list with 4, 6 or 12 members, got " + str(len(a_list)))
def getStepOffset(model): """ Return the current value of step-offset. """ # Get step offset. step_offset = getv(builtin_symbols["step-offset"]) # Check if it is a function, if so, call the function (which cannot take any arguments). if not isinstance(step_offset, numbers.Number): step_offset = getv(step_offset.call(model)) if not isinstance(step_offset, numbers.Number): raise lce.WrongTypeException("number", type(step_offset)) return step_offset
def getArg(self, model, tree, index): """ This will get the arguments one at time. It only works with standard and optional arguments. """ val = getv(interpret(model, tree.value[index + 1])) # Standard arguments. if (index < self.minimum_args): if not isType(val, self.signature[index]): raise lce.WrongTypeException( ", ".join(map(typeToString, self.signature[index])), typeToString(type(val))) return val # All arguments that are beyond the length of the signature are # have to have the type specified by the last argument of the signature. if (index >= len(self.signature)): index = len(self.signature) - 1 if not isType(val, self.signature[index][1]): raise lce.WrongTypeException( ", ".join(map(typeToString, self.signature[index][1])), type(val)) return val
def call(self, model, controlp_list, **kwargs): # Keyword defaults. auto_scale = True if interp.isTrue(kwargs["auto-scale"]) else False extrapolate = True if interp.isTrue(kwargs["extrapolate"]) else False scale = kwargs["scale"] twist = kwargs["twist"] # Process control points. if (len(controlp_list) < 2): raise NumberControlPointsException(len(controlp_list)) control_points = [] for i in range(len(controlp_list)): # Get list of control point vectors. control_point = controlp_list[i] if not isinstance(control_point, list): raise lcadExceptions.WrongTypeException( "list", type(control_point)) if (i == 0): if (len(control_point) != 3): raise ControlPointException( "First control point must include a perpendicular vector." ) else: if (len(control_point) != 2): raise ControlPointException( "Control point must have a location and a derivative (tangent) vector." ) # Process control point vectors. vals = [] for j in range(len(control_point)): vec = control_point[j] if (len(vec) != 3): raise ControlPointException( "Control point vector must have 3 elements") if isinstance(vec, list): for k in range(3): if not isinstance(vec[k], numbers.Number): raise lcadExceptions.WrongTypeException( "number", type(vec[k])) vals.append(vec[k]) elif isinstance(vec, numpy.ndarray): for k in range(3): vals.append(vec[k]) else: raise lcadExceptions.WrongTypeException( "list, numpy.ndarray", type(vec)) # Check that tangent is not zero. tx = vals[3] ty = vals[4] tz = vals[5] if ((tx * tx + ty * ty + tz * tz) < 1.0e-3): raise TangentException() # Create control point. control_points.append(ControlPoint(*vals)) # Create curve. curve = Curve(auto_scale, extrapolate, scale, twist) for i in range(len(control_points) - 1): curve.addSegment(control_points[i], control_points[i + 1]) # Return curve function. return curveFunctions.CurveFunction(curve, "user created curve function.")
def getArgs(self, model, tree): """ This is what you want to use most of the time. It will return either (1) A list containing the standard arguments, followed by the optional arguments or (2) a list containing a list of the standard arguments followed by the keyword dictionary. (1) [standard / optional arguments] (2) [[standard arguments], keyword dictionary] The defaults for the keywords are filled in with the defaults if they are not found. """ args = tree.value[1:] index = 0 # Standard arguments. standard_args = [] while (index < self.minimum_args): val = getv(interpret(model, args[index])) if not isType(val, self.signature[index]): raise lce.WrongTypeException( ", ".join(map(typeToString, self.signature[index])), type(val)) standard_args.append(val) index += 1 # Optional arguments. if self.has_optional_args: sig_index = index while (index < len(args)): val = getv(interpret(model, args[index])) if not isType(val, self.signature[sig_index][1]): raise lce.WrongTypeException( ", ".join( map(typeToString, self.signature[sig_index][1])), type(val)) standard_args.append(val) index += 1 if (sig_index < (len(self.signature) - 2)): sig_index += 1 return standard_args # Keyword arguments. if self.has_keyword_args: sig_dict = self.signature[index][1] # Fill in keyword dictionary. keyword_dict = {} if isinstance(self, UserFunction): for key in sig_dict.keys(): keyword_dict[key] = getv(interpret(model, sig_dict[key][1])) else: for key in sig_dict.keys(): keyword_dict[key] = sig_dict[key][1] # Parse keywords. while (index < len(args)): key = args[index].value if (key[0] != ":"): raise lce.KeywordException(args[index].value) key = key[1:] if not key in sig_dict.keys(): raise lce.UnknownKeywordException(key) val = getv(interpret(model, args[index + 1])) if not isType(val, sig_dict[key][0]): raise lce.WrongTypeException( ", ".join( map(typeToString, self.signature[sig_index][1])), type(val)) keyword_dict[key] = val index += 2 return [standard_args, keyword_dict] return standard_args
def setv(self, value): if isinstance(self.tlist, numpy.ndarray) and not (isinstance(value, numbers.Number)): raise lce.WrongTypeException("number", type(value)) self.tlist[self.index] = value