forked from 1b-agent/opencadstudio-upstream
Merge pull request #1019 from gianlucafiore/feat/pdf-attach
feat(insert): add initial PDF underlay attachment
This commit is contained in:
6 files changed
+157
-1
No files matched your search
@@ -515,11 +515,13 @@ impl OpenCADStudio {
|
||||
self.open_attedit_dialog();
|
||||
}
|
||||
|
||||
"PDFATTACH" => {
|
||||
return Some(Task::done(Message::PdfAttachPick));
|
||||
}
|
||||
"XATTACH" => {
|
||||
// Launch the file picker; XAttachPickResult will start the command.
|
||||
return Some(Task::done(Message::XAttachPick));
|
||||
}
|
||||
|
||||
cmd if cmd == "WBLOCK" || cmd == "WB" || cmd.starts_with("WBLOCK ") => {
|
||||
let arg = cmd.splitn(2, ' ').nth(1).unwrap_or("").trim();
|
||||
if arg.is_empty() {
|
||||
|
||||
@@ -3189,6 +3189,12 @@ pub enum Message {
|
||||
ImagePick,
|
||||
/// Result of the image file picker + pixel dimension decode.
|
||||
ImagePickResult(Result<(std::path::PathBuf, u32, u32), String>),
|
||||
// ── PDF Underlay ──────────────────────────────────────────────────────
|
||||
/// Open file-picker dialog for PDFATTACH command (async).
|
||||
PdfAttachPick,
|
||||
/// Result of the PDFATTACH file picker.
|
||||
PdfAttachPickResult(Result<std::path::PathBuf, String>),
|
||||
|
||||
// ── XREF ──────────────────────────────────────────────────────────────
|
||||
/// Open file-picker dialog for XATTACH command (async).
|
||||
XAttachPick,
|
||||
|
||||
@@ -902,7 +902,60 @@ impl OpenCADStudio {
|
||||
}
|
||||
Task::none()
|
||||
}
|
||||
Message::PdfAttachPick => Task::perform(
|
||||
async {
|
||||
let handle = crate::sys::file_dialog()
|
||||
.set_title("Select PDF Underlay")
|
||||
.add_filter("PDF Files", &["pdf", "PDF"])
|
||||
.pick_file()
|
||||
.await;
|
||||
|
||||
match handle {
|
||||
Some(h) => Ok(crate::sys::handle_path(&h)),
|
||||
None => Err("Cancelled".to_string()),
|
||||
}
|
||||
},
|
||||
Message::PdfAttachPickResult,
|
||||
),
|
||||
|
||||
Message::PdfAttachPickResult(Ok(path)) => {
|
||||
use acadrust::objects::{ObjectType, UnderlayDefinition};
|
||||
use crate::command::CadCommand;
|
||||
use crate::modules::insert::pdf_attach::PdfAttachCommand;
|
||||
|
||||
let i = self.active_tab;
|
||||
let path_str = path.to_string_lossy().into_owned();
|
||||
|
||||
let definition_handle = self.tabs[i].scene.document.allocate_handle();
|
||||
|
||||
let mut definition = UnderlayDefinition::pdf(&path_str, "1");
|
||||
definition.handle = definition_handle;
|
||||
|
||||
self.tabs[i]
|
||||
.scene
|
||||
.document
|
||||
.objects
|
||||
.insert(
|
||||
definition_handle,
|
||||
ObjectType::UnderlayDefinition(definition),
|
||||
);
|
||||
|
||||
let cmd = PdfAttachCommand::new(definition_handle);
|
||||
|
||||
self.command_line.push_info(&cmd.prompt());
|
||||
self.tabs[i].active_cmd = Some(Box::new(cmd));
|
||||
|
||||
Task::none()
|
||||
}
|
||||
|
||||
Message::PdfAttachPickResult(Err(e)) => {
|
||||
if e != "Cancelled" {
|
||||
self.command_line
|
||||
.push_error(crate::tf!("PDFATTACH: {e}").as_ref());
|
||||
}
|
||||
|
||||
Task::none()
|
||||
}
|
||||
Message::XAttachPick => Task::perform(
|
||||
async {
|
||||
let handle = crate::sys::file_dialog()
|
||||
|
||||
@@ -16,6 +16,7 @@ pub mod minsert;
|
||||
mod mview_block;
|
||||
mod open_obj;
|
||||
mod pc_attach;
|
||||
pub(crate) mod pdf_attach;
|
||||
mod snap_underlays;
|
||||
pub(crate) mod solid3d_cmds;
|
||||
mod underlay_layers;
|
||||
@@ -45,6 +46,7 @@ impl CadModule for InsertModule {
|
||||
title: "Reference",
|
||||
tools: vec![
|
||||
RibbonItem::LargeTool(xattach::tool()),
|
||||
RibbonItem::LargeTool(pdf_attach::tool()),
|
||||
RibbonItem::LargeTool(xclip::tool()),
|
||||
RibbonItem::LargeTool(xadjust::tool()),
|
||||
RibbonItem::Tool(underlay_layers::tool()),
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
use acadrust::entities::Underlay;
|
||||
use acadrust::types::{Handle, Vector3};
|
||||
use acadrust::EntityType;
|
||||
use glam::DVec3;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult, WorkingPlane};
|
||||
use crate::modules::{IconKind, ModuleEvent, ToolDef};
|
||||
use crate::t;
|
||||
|
||||
pub const ICON: IconKind =
|
||||
IconKind::Svg(include_bytes!("../../../assets/icons/underlay_layers.svg"));
|
||||
|
||||
pub fn tool() -> ToolDef {
|
||||
ToolDef {
|
||||
id: "PDFATTACH",
|
||||
label: "Attach PDF",
|
||||
icon: ICON,
|
||||
event: ModuleEvent::Command("PDFATTACH".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PdfAttachCommand {
|
||||
definition_handle: Handle,
|
||||
plane: WorkingPlane,
|
||||
}
|
||||
|
||||
impl PdfAttachCommand {
|
||||
pub fn new(definition_handle: Handle) -> Self {
|
||||
Self {
|
||||
definition_handle,
|
||||
plane: WorkingPlane::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CadCommand for PdfAttachCommand {
|
||||
fn set_working_plane(&mut self, plane: WorkingPlane) {
|
||||
self.plane = plane;
|
||||
}
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
"PDFATTACH"
|
||||
}
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
t!("PDFATTACH Specify insertion point:").into_owned()
|
||||
}
|
||||
|
||||
fn on_point(&mut self, pt: DVec3) -> CmdResult {
|
||||
let point = self.plane.to_local(pt);
|
||||
|
||||
let mut underlay = Underlay::pdf();
|
||||
underlay.definition_handle = self.definition_handle;
|
||||
underlay.insertion_point = Vector3::new(point.x, point.y, point.z);
|
||||
|
||||
CmdResult::CommitAndExit(
|
||||
self.plane
|
||||
.place_entity(EntityType::Underlay(underlay)),
|
||||
)
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
}
|
||||
|
||||
inventory::submit!(crate::command::CommandRegistration {
|
||||
names: &["PDFATTACH"]
|
||||
});
|
||||
@@ -1308,5 +1308,29 @@ impl Scene {
|
||||
_ => {}
|
||||
}
|
||||
// The grip-drag caller refreshes changed resident meshes per move.
|
||||
// Raster images, OLE frames and PDF underlays keep a derived ImageModel
|
||||
// with their textured quad geometry. A grip edit mutates the entity directly,
|
||||
// so rebuild that cache as well or only the wire frame follows the grip.
|
||||
let image_bearing = self.document.get_entity(handle).is_some_and(|entity| {
|
||||
matches!(
|
||||
entity,
|
||||
EntityType::RasterImage(_)
|
||||
| EntityType::Ole2Frame(_)
|
||||
| EntityType::Underlay(_)
|
||||
)
|
||||
});
|
||||
|
||||
if image_bearing {
|
||||
let model = self
|
||||
.document
|
||||
.get_entity(handle)
|
||||
.and_then(|entity| self.image_seed_for(entity));
|
||||
|
||||
self.images.remove(&handle);
|
||||
|
||||
if let Some(model) = model {
|
||||
self.images.insert(handle, model);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user