엔티티 만들고 파일에 읽고 쓰기

시리우스 라이브러리에서는 가공 데이타를 자체 Json 파일 포맷으로 저장하거나 읽어들일수있습니다. 이 데이는 IDocument 라는 인터페이스 객체에 저장되며 최상위 컨테이너는 캐드(CAD)편집 프로그램의 레이어(Layer)와 동일한 개체를 제공하고 있습니다.

사용자는 개체(IEntity)를 생성하여 이를 레이어(Layer)에 추가(Add) 하고, 이 레이어(Layer)를 문서(IDocument)에 추가하는 방식을 사용하게 됩니다.

이후 이 문서를 저장(Save/Serialize) 하거나 불러(Deserialize)들일 수 있게 됩니다.

static void Main(string[] args)
{
    SpiralLab.Core.Initialize();
    //신규 문서(Document) 생성
    var doc1 = new DocumentDefault("Unnamed");
    // 레이어 생성
    var layer = new Layer("default");
    //레이어에 선 개체(Entity) 추가
    layer.Add(new Line(0, 10, 20,20));
    //레이어에 원 개체(Entity) 추가
    layer.Add(new Circle(0, 0, 10));
    //레이어에 나선 개체(Entity) 추가
    layer.Add(new Spiral(-20.0f, 0.0f, 0.5f, 2.0f, 5, true));
    // 레이어를 문서에 추가
    doc1.Layers.Add(layer);
    Console.WriteLine("press any key to save ...");
    Console.ReadKey(false);
    string filename = "default.sirius";
    // 문서(Document) 저장하기
    DocumentSerializer.Save(doc1, filename);
    Console.WriteLine("press any key to open ...");
    Console.ReadKey(false);
    // 문서(Document) 불러오기
    var doc2 = DocumentSerializer.OpenSirius(filename);
    Console.WriteLine("press any key to rtc initialize ...");
    Console.ReadKey(false);
    var rtc = new Rtc5(0, "output.txt"); //create Rtc5 controller with list commands output file
    float fov = 60.0f;    // scanner field of view : 60mm            
    float kfactor = (float)Math.Pow(2, 20) / fov; // k factor (bits/mm) = 2^20 / fov
    var correctionFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "correction", "cor_1to1.ct5");
    rtc.Initialize(kfactor, LaserMode.Yag1, correctionFile);   
    rtc.CtlFrequency(50 * 1000, 2); // laser frequency : 50KHz, pulse width : 2usec
    rtc.CtlSpeed(100, 100); // default jump and mark speed : 100mm/s
    rtc.CtlDelay(10, 100, 200, 200, 0); // scanner and laser delays
    ILaser laser = new LaserVirtual(0, "virtual", 20);
    Console.WriteLine("press any key to laser processing ...WARNING !!!  LASER EMISSION");
    Console.ReadKey(false);
    DoBegin(laser, rtc, doc2);
    Console.WriteLine("press any key to terminate program");
    Console.ReadKey(false);
}

static void DoBegin(ILaser laser, IRtc rtc, IDocument doc)
{
    var timer = Stopwatch.StartNew();
    bool success = true;
    var markerArg = new MarkerArgDefault()
    {
        Document = doc,
        Rtc = rtc,
        Laser = laser,
    };
    rtc.ListBegin(laser);
    //레이어를 순회
    foreach (var layer in doc.Layers)
    {
        //레이어 내의 개체(Entity)들을 순회
        foreach (var entity in layer)
        {
            var markerable = entity as IMarkerable;
            //레이저 가공이 가능한 개체(markerable)인지를 판단
            if (null != markerable)
                success &= markerable.Mark(markerArg);    // 해당 개체(Entity) 가공 
            if (!success)
                break;
        }
        if (!success)
            break;
    }
    if (success)
    {
        rtc.ListEnd();
        rtc.ListExecute(true);
    }
    Console.WriteLine($"processing time = {timer.ElapsedMilliseconds / 1000.0:F3}s");
}

댓글 달기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

Scroll to Top