ezrec / uv3dp

Tools for UV Resin based 3D Printers (in Go)
MIT License
58 stars 14 forks source link

Would it be possible to release a library (eg dll) version of this? #142

Open dgm3333 opened 3 years ago

dgm3333 commented 3 years ago

Would it be possible to code a version of uv3dp as a library so it would be feasible to access from other languanges and build an entire ctb file from scratch by loading it with the header, preview, layer header, raw voxel data, etc. Much as I prefer languages like Go and C, I'd like to be able to slice and export files directly from Blender or FreeCAD to improve the flexibilty of individual layer control. However the scripting for these are both in Python. Obviously it would be possible to recode into Python, but then you lose all the multithreading and speed of Go, so it would be a significant step backward.

On the Python side a library would then make it easy to create and send data eg

import ctypes
uv3dp = ctypes.WinDLL('uv3dp.dll')
uv3dp.SetPrinter("sonic-mini-4k")
uv3dp.SetResin("3D-Okay | Black")      # eg loaded from http://bit.ly/2TCLYpJ
uv3dp.SetPreview = previewImage      # (as a plain array or if easier for uv3dp as a more standard image format)
uv3dp.ClearLayers()
layerHeader = uv3dp.layerHeader()
layerData = uv3dp.encodeLayer(slice[n])
uv3dp.AddLayer(layerHeader, layerData)
uv3dp.Save("myModel.ctb")

In Python you can also store binary data using the array module or even pack formated binary data using the struct module (not the same as other languanges' structs) and potentially classes something like the following - although that would probably be overkill as uv3dp is already doing the majority of the heavy lifting required for a really useful library.

from __future__ import annotations    # needed for type annotations until they become standard in Python 3.10
import struct                         # packs data in binary format. typedefs are listed in: https://docs.python.org/3/library/struct.html
from dataclasses import dataclass
import array                          # Packed binary storage of homogeneous data (eg byte array of previews and layers)

@dataclass
class ctbLayerInfo:
    Z        : float    # float32
    Exposure : uv3dpExposure
    Rle      : bytes   # []byte
    Hash     : int    # uint64
    BitsOn   : int    # uint
    packed   : struct

    def pack(self):
        expP = Exposure.pack()
        packed.format = "f" + expP.size() + "s" + len(rle) + "sQI"

        packed.pack(
            ctbLayerInfo ["Z"],
            expP,
            rleP,
            ctbLayerInfo ["Hash"],
            ctbLayerInfo ["BitsOn"]
        )

        return packed

    def size(self):
        return packed.size()
sn4k3 commented 3 years ago

UVtools is all built as a library, but require a C# project, you can import the UVtools.Core.dll There's also the IronPython but i don't know how it works... Also it's possible to call a C# script from python but ugly If you only require ctb format, i have seen some projects in python that you can use to have the file generation For quick stuff and dependency free a light library like uv3dp can make more sense to you

Full Example:

var slicerFile = new ChituboxFile();
slicerFile.BottomLayerCount = BottomLayerCount;
slicerFile.LayerHeight = LayerHeight;
slicerFile.ResolutionX = ResolutionX;
slicerFile.ResolutionY = ResolutionY;
slicerFile.DisplayWidth = DisplayWidth;
slicerFile.DisplayHeight = DisplayHeight;
slicerFile.MachineZ = MachineZ;
slicerFile.MirrorDisplay = MirrorDisplay;
slicerFile.BottomExposureTime = BottomExposureTime;
slicerFile.ExposureTime = ExposureTime;

slicerFile.BottomLiftHeight = BottomLiftHeight;
slicerFile.LiftHeight = LiftHeight;

slicerFile.BottomLiftSpeed = BottomLiftSpeed;
slicerFile.LiftSpeed = LiftSpeed;
slicerFile.RetractSpeed = RetractSpeed;

slicerFile.BottomLightOffDelay = BottomLightOffDelay;
slicerFile.LightOffDelay = LightOffDelay;

slicerFile.BottomLightPWM = BottomLightPWM;
slicerFile.LightPWM = LightPWM;

slicerFile.MachineName = MachineName;
slicerFile.MaterialName = MaterialName;
slicerFile.MaterialMilliliters = MaterialMilliliters;
slicerFile.MaterialGrams = MaterialGrams;
slicerFile.MaterialCost = MaterialCost;

slicerFile.SetThumbnails(Thumbnails);
slicerFile.LayerManager.Layers = Layers;

slicerFile.Encode("filepath.ctb");