--- description: Workflow for preparing IFC models, standardizing filenames, and syncing with APIs & FTP across ATAD software modules --- # Workflow: Prepare IFC Model Deployment This workflow standardizes the IFC integration process across ATAD's software modules. It downloads raw IFC files from Autodesk Forge, unifies their syntax/naming conventions, uploads them via the new 3-step API, updates the database URNs, syncs with FTP, and handles dynamic routing. --- ### Mức 0: Xác định Tiền Tố (Prefix) Khi Agent bắt đầu chạy workflow này để ứng dụng cho một project khác, **phải chủ động hỏi người dùng** về tiền tố sẽ được áp dụng cho tên file IFC đối với dự án hiện tại (Ví dụ: `atad-`, `ins-`, `swe-`, v.v...). 🚫 **TUYỆT ĐỐI KHÔNG** fix cứng chữ `atad-` trong code mẫu. Luôn khai báo động và dùng tiền tố người dùng cung cấp vào tên file đầu ra. ### 1. Khởi tạo Razor Component - Khởi tạo file Component View (thường sẽ là `PrepairIFC.razor` hoặc `PrepareIFC.razor`) với route tương ứng cơ chế của project (VD: `@page "/prepair-ifc/{unitid}"`). - Bắt buộc gắn thuộc tính cấu hình `@attribute [Microsoft.AspNetCore.Authorization.AllowAnonymous]` để cho phép việc trigger/webhook trích xuất tự động không cần xác thực User Auth. - Xây dựng UI Loading thể hiện các bước Process (Đang Authenticate, Đang Load Forge, Uploading API...). - Inject các cấu phần Service: Base DbContext, `IHttpClientFactory`, Service Forge (`IForgeUploadServices`), Service FTP (`IFTPUploadServices`). ### 2. Tiêu Chuẩn Hóa Tên File IFC (Naming Format) Sau khi load dữ liệu DB chứa thông số của Model, tiến hành sinh chuỗi tên File IFC. Tên File tải từ Forge về có thể sai lệch hoặc không tuân thủ mẫu mới nhất. Luôn thực thi cấu trúc: `___.ifc` - `$Prefix`: Truyền biến từ Mức 0. - **Quy tắc Revision**: Khi lấy code version từ DB (VD lấy `IfcModelVersion`), kiểm tra định dạng chữ `R`. Nếu format rỗng hoặc chỉ có số `1`, hệ thống bắt buộc tự convert thành chuẩn 2 char `R01`: ```csharp // Logic mẫu cho AI Agent áp dụng: string rev = dbUnit.IfcModelVersion ?? "00"; if (!rev.StartsWith("R", StringComparison.OrdinalIgnoreCase)) { rev = int.TryParse(rev, out int rInt) ? $"R{rInt:D2}" : $"R{rev}"; } string desiredFilename = $"{prefix}{dbProject.Code}_{dbArea.Code}_{dbUnit.Code}_{rev}.ifc"; ``` ### 3. Tích hợp Autodesk Forge - Gọi hàm Authenticate của Cấu phần Hệ Thống (Ví dụ `ForgeUploadServices.Forge_AuthenticateV2`) để lấy access token. - Khởi tạo `ObjectsApi` gọi `GetObjectsAsync(bucketKey)` để trích xuất ObjectKey cho thư mục Bucket của Model. - Bỏ qua API SDK nếu Download Stream gặp hạn chế (Memory Stream Leak), hãy trực tiếp sử dụng lệnh `HttpClient.GetAsync()` download File thông qua Url tĩnh của Forge (`https://developer.api.autodesk.com/oss/v2/buckets/...`) cùng header *Bearer* để ghi dữ liệu về một Temp Caching Directiory trong Thư mục `Uploads`. ### 4. Luồng API Upload IFC V2 - Truy xuất thông số từ bảng Cấu hình của dự án (Thường là `GLBSystems` với các cột: `ModelApiDomain`, `ModelUserId`, `ModelProjectId`, `ModelFolderId`). - Đẩy File Temp ở Bước 3 qua Multipart Data vào Endpoints API `/api/files/upload`. Đọc JSON trả về lấy `itemId`. (Lưu ý: Status `Conflict` vẫn cần bóc tách đọc ItemID bình thường). - Gọi tiếp sang API Endpoint Lấy `Urn` (`/api/Files/urn?itemId=...`). Lấy ra giá trị `objectUrn` của File 3D. - Kết nối DbContext, Gọi hàm Raw SQL Update `Urn` và cờ View(`IsModelReadyView = 1`) gán qua cho Table chứa cấu trúc lưu trữ 3D tương ứng. ### 5. FTP Server Synchronization (Hệ thống File Server) - Build định dạng đường dẫn lưu trên FTP: `${ProjectCode}/${AreaCode}/${UnitCode}/All/{desiredFilename}`. - Gọi `UploadFile` thao tác đẩy Cache File lên FTP để lưu trữ. - Nếu trả về FtpStatus Success, cập nhật trực tiếp biến Remote Path đó vào cột lưu trữ vật lý của DB. ### 6. Cleanup & Bypass Routing - Xóa Temp Caching File bằng lệnh `File.Delete()` an toàn để giải phóng cấu trúc vùng nhớ và không gian ổ đĩa. - Gọi NavigationManager.NavigateTo bay thẳng qua URL hiển thị mô hình (`/eview/{unit_id}`). Có thể chèn hàm trễ `Task.Delay(2000)` để hiển thị Message báo kết quả Upload xong trước khi đổi trang.