วิธีการใช้ Aspose.CAD ใน Vue

ข้อกำหนดเบื้องต้น

  • Vue CLI
  • Visual Code
  • Node.js

แปลงภาพ dgn เป็น png และแสดงในเบราว์เซอร์

ในตัวอย่างนี้ คุณจะสร้างโปรแกรมการแปลงง่ายๆ ที่แปลงภาพวาดและบันทึกเป็นภาพ

การสร้างโปรเจกต์ Vue

  1. ตรวจสอบให้แน่ใจว่าคุณไม่ได้อยู่ในไดเรกทอรีพื้นที่ทำงาน Vue อยู่แล้ว
  2. เริ่มโปรเจกต์ใหม่และตั้งชื่อโปรแกรมหรือใช้โปรแกรมอื่นๆ ในการสร้างโปรเจกต์ เช่น Visual Code หรือ WebStorm:
    vue create vue-example
  3. ติดตั้ง Aspose.CAD จาก npm package
    npm install aspose-cad
  4. สร้างแท็กไฟล์ประเภท input และ img เพื่อโหลดและแสดงภาพวาด
    <input id="file" type="file">
    <img id="image" />
  5. ใน App.vue เราจะอธิบายขั้นตอนในการเริ่มต้นกระบวนการช่วยเหลือ การประมวลผลและแสดงภาพ
    <script>
    import {Drawing, PngOptions} from "aspose-cad";
    
    export default{
      beforeCreate: function () {
        //จำเป็นสำหรับเริ่มต้นกระบวนการประกอบ
        let recaptchaScript = document.createElement('script')
        recaptchaScript.setAttribute('src', '/node_modules/aspose-cad/dotnet.js')
        document.head.appendChild(recaptchaScript)
    
        let dotnet;
      },
      mounted() {
        window.addEventListener('load', this.onWindowLoad)
      },
      methods: {
        async onWindowLoad() {
          
          console.log("กำลังโหลด WASM...");
          await dotnet.boot();
          console.log("โหลด WASM เสร็จสมบูรณ์");
    
          document.querySelector('input').addEventListener('change', function() {
                const reader = new FileReader();
                reader.onload = function() {
    
                  let arrayBuffer = this.result;
                  let array = new Uint8Array(arrayBuffer);
    
                  // โหลด
                  let file = Image.load(array);
                  console.log(file);
    
                  // บันทึก
                  let exportedFilePromise = Image.save(array, new PngOptions());
                  exportedFilePromise.then(exportedFile => {
                    console.log(exportedFile);
    
                    let urlCreator = window.URL || window.webkitURL;
                    let blob = new Blob([exportedFile], { type: 'application/octet-stream' });
                    let imageUrl = urlCreator.createObjectURL(blob);
                    document.querySelector("#image").src = imageUrl;
                  });
                }
    
                reader.readAsArrayBuffer(this.files[0]);
              },
              false);
        },
      },
    }
    </script>
    
    <template>
      <header>
        <img alt="Vue logo" class="logo" src="./assets/logo.svg" width="125" height="125" />
        <p>ตัวอย่าง aspose.cad สำหรับ Vue.</p>
      </header>
    
      <main>
        <input id="file" type="file">
        <br/>
        <img id="image" />
      </main>
    </template>
    
    <style scoped>
    header {
      line-height: 1.5;
    }
    main{
      text-align: center;
    }
    
    .logo {
      display: block;
      margin: 0 auto 2rem;
    }
    
    @media (min-width: 1024px) {
      header {
        display: flex;
        place-items: center;
        padding-right: calc(var(--section-gap) / 2);
      }
    
    
      header .wrapper {
        display: flex;
        place-items: flex-start;
        flex-wrap: wrap;
      }
    }
    </style>
  6. เริ่มแอปพลิเคชัน
    npm run dev

ตัวอย่างการทำงาน

  1. เลือกไฟล์.
    เลือกไฟล์
  2. เลือกไฟล์ DXF, DWG, DGN, DWF, DWFX, IFC, STL, DWT, IGES, PLT, CF2, OBJ, HPGL, IGS, PCL, FBX, PDF, SVG ใด ๆ
  3. หากการตอบกลับสำเร็จ ไฟล์จะแสดงบนหน้าจอและเสนอให้ดาวน์โหลด
    แปลงภาพ

ดูเพิ่มเติม