Пример #1
0
def main(obj, ply):
    '''if (args.from_up is None) != (args.to_up is None):
        raise Exception("from-up and to-up args should be both present or both absent")

    up_conversion = None
    if args.from_up is not None:
        up_conversion = (args.from_up, args.to_up)

    output = args.output if args.output is not None else '.' + args.type'''

    result = mt.convert(obj, ply)

    #if args.output is None:
    print(result)
    with open(ply, 'w') as f:
        f.write(result)
Пример #2
0
def main(args):

    if (args.from_up is None) != (args.to_up is None):
        raise Exception(
            "from-up and to-up args should be both present or both absent")

    up_conversion = None
    if args.from_up is not None:
        up_conversion = (args.from_up, args.to_up)

    output = args.output if args.output is not None else '.' + args.type

    result = mt.convert(args.input, output, up_conversion)

    if args.output is None:
        print(result)
    else:
        with open(args.output, 'w') as f:
            f.write(result)
Пример #3
0
    def effect(self):
        inputfile = self.options.inputfile
        if not os.path.exists(inputfile):
            inkex.utils.debug(
                "The input file does not exist. Please select a proper file and try again."
            )
            exit(1)
        converted_inputfile = os.path.join(
            tempfile.gettempdir(),
            os.path.splitext(os.path.basename(inputfile))[0] + ".stl")
        if os.path.exists(converted_inputfile):
            os.remove(converted_inputfile
                      )  #remove previously generated conversion file
        up_conversion = None
        with open(converted_inputfile, 'w') as f:
            f.write(mt.convert(inputfile, converted_inputfile, up_conversion))

        # Run ADMesh mesh fixer to overwrite the STL with fixed output and binary file format for osresearch/papercraft
        if os.name == "nt":
            admesh_cmd = "admesh\\windows\\admesh.exe "
        else:
            admesh_cmd = "./admesh/linux/admesh "

        if self.options.xy_mirror == True: admesh_cmd += "--xy-mirror "
        if self.options.yz_mirror == True: admesh_cmd += "--yz-mirror "
        if self.options.xz_mirror == True: admesh_cmd += "--xz-mirror "
        if self.options.scale != 1.0:
            admesh_cmd += "--scale " + str(self.options.scale) + " "
        if self.options.exact == True: admesh_cmd += "--exact "
        if self.options.nearby == True: admesh_cmd += "--nearby "
        if self.options.tolerance > 0.0:
            admesh_cmd += "--tolerance " + str(self.options.tolerance) + " "
        if self.options.iterations > 1:
            admesh_cmd += "--iterations " + str(self.options.iterations) + " "
        if self.options.increment > 0.0:
            admesh_cmd += "--increment " + str(self.options.increment) + " "
        if self.options.remove_unconnected == True:
            admesh_cmd += "--remove-unconnected "
        if self.options.normal_directions == True:
            admesh_cmd += "--normal-directions "
        if self.options.fill_holes == True: admesh_cmd += "--fill-holes "
        if self.options.reverse_all == True: admesh_cmd += "--reverse-all "
        if self.options.normal_values == True: admesh_cmd += "--normal-values "
        admesh_cmd += "\"" + converted_inputfile + "\" "
        admesh_cmd += "-b \"" + converted_inputfile + "\""
        p = Popen(admesh_cmd, shell=True, stdout=PIPE, stderr=PIPE)
        stdout, stderr = p.communicate()
        p.wait()
        if p.returncode != 0:
            inkex.utils.debug("admesh failed: %d %s %s" %
                              (p.returncode, stdout, stderr))
            exit(1)

        # Run papercraft flattening
        converted_flattenfile = os.path.join(
            tempfile.gettempdir(),
            os.path.splitext(os.path.basename(inputfile))[0] + ".svg")
        if os.path.exists(converted_flattenfile):
            os.remove(converted_flattenfile
                      )  #remove previously generated conversion file
        if self.options.generatelabels:
            unfold_exec = "unfold_labels"
        else:
            unfold_exec = "unfold_nolabels"
        if os.name == "nt":
            papercraft_cmd = "unfold\\" + unfold_exec + ".exe" + " < \"" + converted_inputfile + "\" > \"" + converted_flattenfile + "\""
        else:
            papercraft_cmd = "./unfold/" + unfold_exec + " < \"" + converted_inputfile + "\" > \"" + converted_flattenfile + "\""
        p = Popen(papercraft_cmd, shell=True, stdout=PIPE, stderr=PIPE)
        stdout, stderr = p.communicate()
        p.wait()
        if p.returncode != 0:
            inkex.utils.debug("osresearch/papercraft unfold failed: %d %s %s" %
                              (p.returncode, stdout, stderr))

        # Open converted output in fstl
        if self.options.show_fstl == True:
            if os.name == "nt":
                fstl_cmd = "fstl\\fstl.exe \"" + converted_inputfile + "\""
            else:
                fstl_cmd = "./fstl/fstl \"" + converted_inputfile + "\""
            p = Popen(fstl_cmd, shell=True)
            p.wait()

        # Write the generated SVG into InkScape's canvas
        try:
            stream = open(converted_flattenfile, 'r')
        except FileNotFoundError as e:
            inkex.utils.debug(
                "There was no SVG output generated. Cannot continue")
            exit(1)
        p = etree.XMLParser(huge_tree=True)
        doc = etree.parse(stream,
                          parser=etree.XMLParser(huge_tree=True)).getroot()
        stream.close()
        doc.set('id', self.svg.get_unique_id('papercraft_unfold'))
        self.document.getroot().append(doc)

        #adjust viewport and width/height to have the import at the center of the canvas
        if self.options.resizetoimport:
            bbox = inkex.elements._selected.ElementList.bounding_box(doc)
            if bbox is not None:
                root = self.svg.getElement('//svg:svg')
                offset = self.svg.unittouu(
                    str(self.options.extraborder) +
                    self.options.extraborder_units)
                root.set(
                    'viewBox', '%f %f %f %f' %
                    (bbox.left - offset, bbox.top - offset,
                     bbox.width + 2 * offset, bbox.height + 2 * offset))
                root.set('width', bbox.width + 2 * offset)
                root.set('height', bbox.height + 2 * offset)