forked from 1b-agent/opencadstudio-upstream
Merge pull request #1484 from ramox81/codex/pdf-import-followups
PDF import and underlay follow-ups
This commit is contained in:
commit
e2e2c36b6a
12 files changed
+412
-83
No files matched your search
@@ -622,6 +622,7 @@ inventory::submit!(crate::command::CommandRegistration {
|
||||
"PDFIMPORTMODE",
|
||||
"PDFIMPORTFILTER",
|
||||
"PDFIMPORTLAYERS",
|
||||
"PDFIMPORTIMAGEPATH",
|
||||
"XDWGFADECTL",
|
||||
"POINTCLOUDCLIPFRAME",
|
||||
"XCLIPFRAME",
|
||||
|
||||
@@ -77,18 +77,12 @@ impl OpenCADStudio {
|
||||
};
|
||||
let naming = pdf_import::LayerNaming {
|
||||
settings,
|
||||
prefix: import_prefix(&path),
|
||||
prefix: import_prefix(),
|
||||
current: current_layer.clone(),
|
||||
};
|
||||
let mut result = pdf_import::convert(&content, &underlay, &area, &naming);
|
||||
let images = std::mem::take(&mut result.images);
|
||||
let image_dir = self.tabs[i]
|
||||
.current_path
|
||||
.as_deref()
|
||||
.and_then(|p| p.parent())
|
||||
.map(|p| p.to_path_buf())
|
||||
.or_else(|| std::path::Path::new(&path).parent().map(|p| p.to_path_buf()))
|
||||
.map(|dir| dir.join("PDF Images"));
|
||||
let image_dir = pdf_import::image_dir(&path);
|
||||
|
||||
self.push_undo_snapshot(i, "PDFIMPORT");
|
||||
let scene = &mut self.tabs[i].scene;
|
||||
@@ -100,8 +94,8 @@ impl OpenCADStudio {
|
||||
lt.pattern_length = 1.0;
|
||||
let _ = scene.document.line_types.add(lt);
|
||||
}
|
||||
// Raster images: each written as a PNG under "PDF Images" next to the
|
||||
// drawing (or the PDF while the drawing is unsaved), then referenced.
|
||||
// Raster images: each written as a PNG in the PDFIMPORTIMAGEPATH
|
||||
// folder, named after the PDF with 8 hex digits, then referenced.
|
||||
let stem = std::path::Path::new(&path.replace('\\', "/"))
|
||||
.file_stem()
|
||||
.map(|s| s.to_string_lossy().into_owned())
|
||||
@@ -117,8 +111,14 @@ impl OpenCADStudio {
|
||||
{
|
||||
continue;
|
||||
}
|
||||
// Rebuilt from its components: one separator style, the platform's.
|
||||
let stored = file
|
||||
.components()
|
||||
.collect::<std::path::PathBuf>()
|
||||
.to_string_lossy()
|
||||
.into_owned();
|
||||
let mut raster = codec::entities::RasterImage::with_size(
|
||||
file.to_string_lossy().as_ref(),
|
||||
&stored,
|
||||
image.insertion,
|
||||
image.width as f64,
|
||||
image.height as f64,
|
||||
@@ -128,7 +128,9 @@ impl OpenCADStudio {
|
||||
raster.u_vector = image.u;
|
||||
raster.v_vector = image.v;
|
||||
raster.flags = codec::entities::ImageDisplayFlags::SHOW_IMAGE
|
||||
| codec::entities::ImageDisplayFlags::USE_CLIPPING_BOUNDARY;
|
||||
| codec::entities::ImageDisplayFlags::SHOW_NOT_ALIGNED
|
||||
| codec::entities::ImageDisplayFlags::USE_CLIPPING_BOUNDARY
|
||||
| codec::entities::ImageDisplayFlags::TRANSPARENCY_ON;
|
||||
raster.common.layer = image.layer.clone();
|
||||
result.entities.push(codec::EntityType::RasterImage(raster));
|
||||
}
|
||||
@@ -154,8 +156,8 @@ impl OpenCADStudio {
|
||||
let mut name = stem.clone();
|
||||
let mut n = 1;
|
||||
while scene.document.block_records.get(&name).is_some() {
|
||||
name = format!("{stem}{n}");
|
||||
n += 1;
|
||||
name = format!("{stem}({n})");
|
||||
}
|
||||
if scene
|
||||
.define_block_from_owned_entities(result.entities, &name, glam::DVec3::ZERO)
|
||||
@@ -239,24 +241,15 @@ impl OpenCADStudio {
|
||||
}
|
||||
}
|
||||
|
||||
/// The layer prefix of an imported file: "PDF_" for the first file of the
|
||||
/// session, "PDF2_", "PDF3_" … for each other file.
|
||||
// ponytail: remembered for the session only; a reopened drawing starts from
|
||||
/// The layer prefix of an import: "PDF_" for the first import of the
|
||||
/// session, "PDF2_", "PDF3_" … for each later one, of the same file or not.
|
||||
// ponytail: counted for the session only; a reopened drawing starts from
|
||||
// "PDF_" again.
|
||||
fn import_prefix(path: &str) -> String {
|
||||
static FILES: std::sync::Mutex<Vec<String>> = std::sync::Mutex::new(Vec::new());
|
||||
let mut files = FILES.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let index = match files.iter().position(|f| f.eq_ignore_ascii_case(path)) {
|
||||
Some(at) => at,
|
||||
None => {
|
||||
files.push(path.to_string());
|
||||
files.len() - 1
|
||||
}
|
||||
};
|
||||
if index == 0 {
|
||||
"PDF_".to_string()
|
||||
} else {
|
||||
format!("PDF{}_", index + 1)
|
||||
fn import_prefix() -> String {
|
||||
static IMPORTS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
|
||||
match IMPORTS.fetch_add(1, std::sync::atomic::Ordering::Relaxed) {
|
||||
0 => "PDF_".to_string(),
|
||||
n => format!("PDF{}_", n + 1),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1022,6 +1022,7 @@ impl OpenCADStudio {
|
||||
| "PDFIMPORTMODE"
|
||||
| "PDFIMPORTFILTER"
|
||||
| "PDFIMPORTLAYERS"
|
||||
| "PDFIMPORTIMAGEPATH"
|
||||
| "XDWGFADECTL"
|
||||
| "POINTCLOUDCLIPFRAME"
|
||||
| "XCLIPFRAME"
|
||||
@@ -1158,6 +1159,24 @@ impl OpenCADStudio {
|
||||
}
|
||||
return Some(self.finish_dispatch(cmd));
|
||||
}
|
||||
// A folder name; "." clears it.
|
||||
if name == "PDFIMPORTIMAGEPATH" {
|
||||
use crate::modules::insert::pdf_import::{image_path, set_image_path};
|
||||
match &value {
|
||||
Some(value) => {
|
||||
let value = value.trim().trim_matches('"');
|
||||
set_image_path(if value == "." { String::new() } else { value.to_string() });
|
||||
}
|
||||
None => {
|
||||
self.command_line.push_output(&format!(
|
||||
"Enter new value for PDFIMPORTIMAGEPATH, or . for none <\"{}\">:",
|
||||
image_path()
|
||||
));
|
||||
self.pending_setvar = Some(name.clone());
|
||||
}
|
||||
}
|
||||
return Some(self.finish_dispatch(cmd));
|
||||
}
|
||||
if matches!(name.as_str(), "PDFOSNAP" | "UOSNAP") {
|
||||
let current = i16::from(crate::scene::model::pdf_vector::pdf_osnap());
|
||||
if let Some(value) = &value {
|
||||
|
||||
@@ -459,6 +459,7 @@ impl OpenCADStudio {
|
||||
| "commands"
|
||||
| "properties"
|
||||
| "measure"
|
||||
| "snap"
|
||||
| "query"
|
||||
| "records"
|
||||
| "record_schema"
|
||||
@@ -705,6 +706,28 @@ impl OpenCADStudio {
|
||||
let response = match op {
|
||||
"properties" => self.control_properties(),
|
||||
"measure" => self.control_measure(&req),
|
||||
// The object snap a cursor over `point` (world) would get;
|
||||
// `from` is the base for perpendicular and tangent.
|
||||
"snap" => {
|
||||
let p = &req["point"];
|
||||
let world = glam::DVec3::new(
|
||||
p[0].as_f64().unwrap_or(0.0),
|
||||
p[1].as_f64().unwrap_or(0.0),
|
||||
p[2].as_f64().unwrap_or(0.0),
|
||||
);
|
||||
let from = req["from"].as_array().map(|f| {
|
||||
glam::DVec3::new(
|
||||
f.first().and_then(|v| v.as_f64()).unwrap_or(0.0),
|
||||
f.get(1).and_then(|v| v.as_f64()).unwrap_or(0.0),
|
||||
f.get(2).and_then(|v| v.as_f64()).unwrap_or(0.0),
|
||||
)
|
||||
});
|
||||
let i = self.active_tab;
|
||||
match self.snap_query(i, world, from) {
|
||||
Some(hit) => json!({"ok":true,"snap":format!("{:?}", hit.snap_type),"world":[hit.world.x,hit.world.y,hit.world.z]}),
|
||||
None => json!({"ok":true,"snap":null}),
|
||||
}
|
||||
}
|
||||
"xdata_get" => self.xdata_read(&req),
|
||||
"get_selection" => self.control_get_selection(),
|
||||
"history" => {
|
||||
|
||||
@@ -2872,6 +2872,49 @@ impl OpenCADStudio {
|
||||
)
|
||||
}
|
||||
|
||||
/// The object snap the cursor would get over `world` in the model view,
|
||||
/// through the same candidate gathering and snapper as a cursor move
|
||||
/// (automation's `snap` query).
|
||||
pub(in crate::app) fn snap_query(
|
||||
&mut self,
|
||||
i: usize,
|
||||
world: glam::DVec3,
|
||||
from: Option<glam::DVec3>,
|
||||
) -> Option<crate::snap::SnapResult> {
|
||||
let (vw, vh) = self.tabs[i].scene.selection.borrow().vp_size;
|
||||
if vw <= 1.0 || vh <= 1.0 {
|
||||
return None;
|
||||
}
|
||||
let tile_b = self.tabs[i].scene.active_model_tile_bounds(vw, vh);
|
||||
let bounds = iced::Rectangle {
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
width: tile_b.width,
|
||||
height: tile_b.height,
|
||||
};
|
||||
let (view_rot, eye, px) = {
|
||||
let cam = self.tabs[i].scene.camera.borrow();
|
||||
(cam.view_proj_rte(bounds), cam.eye(), cam.project(world, bounds)?)
|
||||
};
|
||||
let p = Point::new(px.x, px.y);
|
||||
let raw = self.cursor_model_point(i, &None, p, bounds);
|
||||
let all_wires = self.tabs[i].scene.hit_test_wires();
|
||||
let candidates = self.tabs[i].scene.interaction_candidates_near(
|
||||
all_wires,
|
||||
raw,
|
||||
view_rot,
|
||||
eye,
|
||||
bounds,
|
||||
self.snapper.osnap_radius_px,
|
||||
);
|
||||
// Perpendicular and tangent measure from `from`, else the running
|
||||
// command's last point.
|
||||
self.snapper.from_point = from.or(self.last_point).map(|p| p.as_vec3());
|
||||
let (go, gr) = self.drafting_grid_basis(i);
|
||||
self.snapper
|
||||
.snap(raw, p, &candidates, view_rot, eye, bounds, go, gr, None)
|
||||
}
|
||||
|
||||
/// Apply one frame of a UCS-icon grip drag: map the cursor onto the active
|
||||
/// UCS plane (so the move stays in-plane) and either slide the origin there
|
||||
/// or rotate the chosen axis to point at it, keeping a right-handed frame
|
||||
|
||||
+2
-1
@@ -35,6 +35,7 @@ const READ_OPS: &[&str] = &[
|
||||
"header",
|
||||
"properties",
|
||||
"measure",
|
||||
"snap",
|
||||
"history",
|
||||
"commands",
|
||||
"events",
|
||||
@@ -1198,7 +1199,7 @@ fn tool_definitions() -> Value {
|
||||
{
|
||||
"name":"ocs_read",
|
||||
"description":"Discover capabilities and record schemas, or read state, complete database records, command manifests, entities, properties, kernel measurements and spatial relationships, history, events or operation status from a live OCS session.",
|
||||
"inputSchema":{"type":"object","properties":{"ocs_session_id":{"type":"string","minLength":1,"description":"Value of session_id returned by ocs_sessions."},"op":{"type":"string","enum":READ_OPS,"default":"state"},"parameters":{"type":"object","description":"Operation-specific filters.","properties":{"name":{"type":"string","description":"Command name or record name."},"search":{"type":"string","description":"Case-insensitive command or record-type search."},"document_id":{"type":"integer","minimum":0},"path":{"type":"string","description":"Optional intended output path for audit; extension determines target format."},"target_format":{"type":"string","enum":["dwg","dxf"],"description":"Intended output format for audit."},"target_version":{"type":"string","enum":["R14","2000","2004","2007","2010","2013","2018","AC1014","AC1015","AC1018","AC1021","AC1024","AC1027","AC1032"],"description":"Intended CAD output version for audit."},"collection":{"type":"string","description":"Record collection, all for records, or omit to discover collections and schema types."},"handle":{"type":"string"},"handles":{"type":"array","items":{"type":"string"},"description":"Exact entity or record handles."},"type":{"type":"string","description":"Entity or record type filter; for record_schema, returns its complete type graph and writable field paths."},"layer":{"type":"string","description":"Layer name filter for query."},"detail":{"type":"string","enum":["summary","geometry","full"],"default":"geometry","description":"Entity detail returned by query."},"fields":{"type":"array","items":{"type":"string"},"description":"Return only these entity fields plus handle."},"paths":{"type":"array","items":{"type":"string"},"description":"Project RFC 6901 JSON Pointer paths relative to record.properties."},"where":{"type":"array","description":"All property filters must match.","items":{"type":"object","properties":{"path":{"type":"string"},"op":{"type":"string","enum":["eq","ne","lt","lte","gt","gte","contains","starts_with","ends_with","in","exists","not_exists"],"default":"eq"},"value":{}},"required":["path"],"additionalProperties":false}},"near":{"type":"array","items":{"type":"number"},"minItems":2,"maxItems":3,"description":"Rank planar curves by exact kernel distance to this world XY point."},"contains_point":{"type":"array","items":{"type":"number"},"minItems":2,"maxItems":3,"description":"Return closed planar curves containing this world XY point."},"bounds":{"type":"array","items":{"type":"number"},"minItems":4,"maxItems":4,"description":"Filter entities whose world XY bounds overlap [min_x,min_y,max_x,max_y]."},"intersections":{"type":"array","items":{"type":"string"},"minItems":2,"maxItems":2,"description":"Return exact kernel intersections between two planar curve handles."},"after":{"type":"integer","minimum":0,"description":"Event cursor."},"request_id":{"type":"string","description":"Operation id to query."},"offset":{"type":"integer","minimum":0},"limit":{"type":"integer","minimum":1,"maximum":10000}},"additionalProperties":false}},"required":["ocs_session_id"],"additionalProperties":false},
|
||||
"inputSchema":{"type":"object","properties":{"ocs_session_id":{"type":"string","minLength":1,"description":"Value of session_id returned by ocs_sessions."},"op":{"type":"string","enum":READ_OPS,"default":"state"},"parameters":{"type":"object","description":"Operation-specific filters.","properties":{"name":{"type":"string","description":"Command name or record name."},"search":{"type":"string","description":"Case-insensitive command or record-type search."},"document_id":{"type":"integer","minimum":0},"path":{"type":"string","description":"Optional intended output path for audit; extension determines target format."},"target_format":{"type":"string","enum":["dwg","dxf"],"description":"Intended output format for audit."},"target_version":{"type":"string","enum":["R14","2000","2004","2007","2010","2013","2018","AC1014","AC1015","AC1018","AC1021","AC1024","AC1027","AC1032"],"description":"Intended CAD output version for audit."},"collection":{"type":"string","description":"Record collection, all for records, or omit to discover collections and schema types."},"handle":{"type":"string"},"handles":{"type":"array","items":{"type":"string"},"description":"Exact entity or record handles."},"type":{"type":"string","description":"Entity or record type filter; for record_schema, returns its complete type graph and writable field paths."},"layer":{"type":"string","description":"Layer name filter for query."},"detail":{"type":"string","enum":["summary","geometry","full"],"default":"geometry","description":"Entity detail returned by query."},"fields":{"type":"array","items":{"type":"string"},"description":"Return only these entity fields plus handle."},"paths":{"type":"array","items":{"type":"string"},"description":"Project RFC 6901 JSON Pointer paths relative to record.properties."},"where":{"type":"array","description":"All property filters must match.","items":{"type":"object","properties":{"path":{"type":"string"},"op":{"type":"string","enum":["eq","ne","lt","lte","gt","gte","contains","starts_with","ends_with","in","exists","not_exists"],"default":"eq"},"value":{}},"required":["path"],"additionalProperties":false}},"near":{"type":"array","items":{"type":"number"},"minItems":2,"maxItems":3,"description":"Rank planar curves by exact kernel distance to this world XY point."},"point":{"type":"array","items":{"type":"number"},"minItems":2,"maxItems":3,"description":"World point whose object snap the snap op reports."},"from":{"type":"array","items":{"type":"number"},"minItems":2,"maxItems":3,"description":"Base point for perpendicular and tangent snaps (snap op)."},"contains_point":{"type":"array","items":{"type":"number"},"minItems":2,"maxItems":3,"description":"Return closed planar curves containing this world XY point."},"bounds":{"type":"array","items":{"type":"number"},"minItems":4,"maxItems":4,"description":"Filter entities whose world XY bounds overlap [min_x,min_y,max_x,max_y]."},"intersections":{"type":"array","items":{"type":"string"},"minItems":2,"maxItems":2,"description":"Return exact kernel intersections between two planar curve handles."},"after":{"type":"integer","minimum":0,"description":"Event cursor."},"request_id":{"type":"string","description":"Operation id to query."},"offset":{"type":"integer","minimum":0},"limit":{"type":"integer","minimum":1,"maximum":10000}},"additionalProperties":false}},"required":["ocs_session_id"],"additionalProperties":false},
|
||||
"outputSchema":read_output_schema(),
|
||||
"annotations":{"title":"Read OCS state","readOnlyHint":true,"destructiveHint":false,"idempotentHint":true,"openWorldHint":false}
|
||||
},
|
||||
|
||||
@@ -92,7 +92,7 @@ pub struct PdfAttachCommand {
|
||||
path: String,
|
||||
/// The path the definition stores (full, relative or file name only).
|
||||
stored_path: Option<String>,
|
||||
/// Pages after the first, attached beside it (dialog selection).
|
||||
/// Pages after the first, attached at the same point (dialog selection).
|
||||
extra_pages: Vec<String>,
|
||||
/// Scale and rotation fixed in the dialog: their prompts are skipped.
|
||||
preset_scale: Option<f64>,
|
||||
@@ -301,7 +301,6 @@ impl PdfAttachCommand {
|
||||
.chain(self.extra_pages.iter().cloned())
|
||||
.collect();
|
||||
let placed = underlays_for_pages(
|
||||
&self.path,
|
||||
&pages,
|
||||
self.plane.to_local(self.insertion),
|
||||
self.scale,
|
||||
@@ -427,24 +426,19 @@ impl CadCommand for PdfAttachCommand {
|
||||
}
|
||||
}
|
||||
|
||||
/// Underlays for `pages` of a file: the first at `insertion`, each next one
|
||||
/// beside the previous along the rotated X axis (page width times scale).
|
||||
// ponytail: side-by-side placement of the pages after the first is not
|
||||
// measured against the reference.
|
||||
/// Underlays for `pages` of a file, all at `insertion` with the same scale
|
||||
/// and rotation (the reference stacks the chosen pages there).
|
||||
pub fn underlays_for_pages(
|
||||
read_path: &str,
|
||||
pages: &[String],
|
||||
insertion: DVec3,
|
||||
scale: f64,
|
||||
rotation: f64,
|
||||
) -> Vec<(String, EntityType)> {
|
||||
let mut origin = insertion;
|
||||
let (c, s) = (rotation.cos(), rotation.sin());
|
||||
pages
|
||||
.iter()
|
||||
.map(|page| {
|
||||
let mut underlay = Underlay::pdf();
|
||||
underlay.insertion_point = Vector3::new(origin.x, origin.y, origin.z);
|
||||
underlay.insertion_point = Vector3::new(insertion.x, insertion.y, insertion.z);
|
||||
underlay.set_scale(scale);
|
||||
underlay.rotation = rotation;
|
||||
// On, clipped by its boundary and colour-adjusted for the
|
||||
@@ -452,9 +446,6 @@ pub fn underlays_for_pages(
|
||||
underlay.flags = UnderlayDisplayFlags::ON
|
||||
| UnderlayDisplayFlags::CLIPPING
|
||||
| UnderlayDisplayFlags::ADJUST_FOR_BACKGROUND;
|
||||
let width = crate::scene::model::pdf_raster::page_size_inches(read_path, page)
|
||||
.map_or(0.0, |(w, _)| w * scale);
|
||||
origin += DVec3::new(c * width, s * width, 0.0);
|
||||
(page.clone(), EntityType::Underlay(underlay))
|
||||
})
|
||||
.collect()
|
||||
|
||||
@@ -9,13 +9,14 @@
|
||||
// Content of a PDF layer goes to PDF_<layer>; the rest to PDF_Geometry,
|
||||
// PDF_Solid Fills, PDF_Text and PDF_Images (or those alone, or the current
|
||||
// layer, per the settings). Straight strokes that meet become polylines, a
|
||||
// closed four-arc Bézier loop that is a circle a CIRCLE, other curves
|
||||
// splines, filled triangles and quadrilaterals SOLIDs at 50% transparency
|
||||
// Bézier that is a circular arc an arc segment of them (with "Join" on) or
|
||||
// an ARC (off), a closed four-arc Bézier loop that is a circle a CIRCLE,
|
||||
// other curves splines, filled triangles and quadrilaterals SOLIDs at 50% transparency
|
||||
// (other fills solid hatches), text runs MTEXT in a "PDF <font>" style and
|
||||
// raster images PNG files referenced by IMAGE objects.
|
||||
|
||||
use codec::entities::{
|
||||
AttachmentPoint, BoundaryEdge, BoundaryPath, Circle, Hatch, LwPolyline, MText, PolylineEdge,
|
||||
Arc, AttachmentPoint, BoundaryEdge, BoundaryPath, Circle, Hatch, LwPolyline, MText, PolylineEdge,
|
||||
Solid, Spline, Underlay, UnderlayType,
|
||||
};
|
||||
use codec::types::{Color, Handle, LineWeight, Vector2, Vector3};
|
||||
@@ -133,6 +134,35 @@ pub fn set_import_settings(settings: PdfImportSettings) {
|
||||
}
|
||||
}
|
||||
|
||||
static IMAGE_PATH: std::sync::Mutex<Option<String>> = std::sync::Mutex::new(None);
|
||||
|
||||
/// PDFIMPORTIMAGEPATH: the folder raster images are extracted to — relative
|
||||
/// to the folder of the PDF, absolute as given, empty for that folder itself.
|
||||
pub fn image_path() -> String {
|
||||
IMAGE_PATH
|
||||
.lock()
|
||||
.ok()
|
||||
.and_then(|p| p.clone())
|
||||
.unwrap_or_else(|| "PDF Images".to_string())
|
||||
}
|
||||
|
||||
pub fn set_image_path(path: String) {
|
||||
if let Ok(mut p) = IMAGE_PATH.lock() {
|
||||
*p = Some(path);
|
||||
}
|
||||
}
|
||||
|
||||
/// The extraction folder for the PDF at `pdf`.
|
||||
pub fn image_dir(pdf: &str) -> Option<std::path::PathBuf> {
|
||||
let setting = image_path();
|
||||
let folder = std::path::Path::new(&setting);
|
||||
if folder.is_absolute() {
|
||||
return Some(folder.to_path_buf());
|
||||
}
|
||||
let parent = std::path::Path::new(pdf).parent()?;
|
||||
Some(if setting.is_empty() { parent.to_path_buf() } else { parent.join(folder) })
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum ImportArea {
|
||||
All,
|
||||
@@ -557,27 +587,75 @@ pub struct ImageImport {
|
||||
/// space 0.2, scaled per object to the dash length.
|
||||
pub const DASH_LINETYPE: &str = "PDF_IMPORT";
|
||||
|
||||
/// A stroke before it becomes an object: straight chains can still be joined
|
||||
/// or read as dashes.
|
||||
/// A stroke before it becomes an object: chains can still be joined or read
|
||||
/// as dashes. `bulges[i]` bends the segment from point i to the next (the
|
||||
/// closing one on a closed stroke; 0 after an open stroke's last point).
|
||||
struct Stroke {
|
||||
layer: String,
|
||||
color: Color,
|
||||
weight: LineWeight,
|
||||
points: Vec<[f64; 2]>,
|
||||
bulges: Vec<f64>,
|
||||
closed: bool,
|
||||
}
|
||||
|
||||
impl Stroke {
|
||||
fn straight(&self) -> bool {
|
||||
self.bulges.iter().all(|b| *b == 0.0)
|
||||
}
|
||||
|
||||
fn reverse(&mut self) {
|
||||
self.points.reverse();
|
||||
let n = self.bulges.len();
|
||||
let mut bulges: Vec<f64> = self.bulges[..n - 1].iter().rev().map(|b| -b).collect();
|
||||
bulges.push(0.0);
|
||||
self.bulges = bulges;
|
||||
}
|
||||
}
|
||||
|
||||
/// The bulge of a Bézier that is a circular arc (its quarter, half and
|
||||
/// three-quarter points on the circle through its ends and midpoint), from
|
||||
/// its midpoint's height over the chord; positive counter-clockwise.
|
||||
fn arc_bulge(seg: &Segment) -> Option<f64> {
|
||||
let Segment::Cubic(p0, _, _, p3) = seg else {
|
||||
return None;
|
||||
};
|
||||
let m = bezier(seg, 0.5);
|
||||
let (ax, ay) = (m[0] - p0[0], m[1] - p0[1]);
|
||||
let (bx, by) = (p3[0] - p0[0], p3[1] - p0[1]);
|
||||
let d = 2.0 * (ax * by - ay * bx);
|
||||
let chord = bx.hypot(by);
|
||||
if d.abs() < 1e-12 || chord < 1e-9 {
|
||||
return None;
|
||||
}
|
||||
// Circle through p0, m, p3 (relative to p0).
|
||||
let (a2, b2) = (ax * ax + ay * ay, bx * bx + by * by);
|
||||
let c = [(by * a2 - ay * b2) / d, (ax * b2 - bx * a2) / d];
|
||||
let r = c[0].hypot(c[1]);
|
||||
let tol = r * 2e-3;
|
||||
let on = [0.25, 0.75].iter().all(|t| {
|
||||
let q = bezier(seg, *t);
|
||||
((q[0] - p0[0] - c[0]).hypot(q[1] - p0[1] - c[1]) - r).abs() <= tol
|
||||
});
|
||||
if !on {
|
||||
return None;
|
||||
}
|
||||
let h = (ax * by - ay * bx) / chord;
|
||||
Some(2.0 * h / chord)
|
||||
}
|
||||
|
||||
fn same_point(a: [f64; 2], b: [f64; 2]) -> bool {
|
||||
(a[0] - b[0]).abs() < 1e-7 && (a[1] - b[1]).abs() < 1e-7
|
||||
}
|
||||
|
||||
/// Open straight strokes of one layer, colour and weight that meet end to
|
||||
/// end become one polyline (closed when the chain returns to its start).
|
||||
fn join_strokes(strokes: Vec<Stroke>) -> Vec<Stroke> {
|
||||
/// Open strokes of one layer, colour and weight that meet end to end become
|
||||
/// one polyline (closed when the chain returns to its start). Straight ones
|
||||
/// always do; ones with arcs only with `join` (off, their arcs stay apart).
|
||||
fn join_strokes(strokes: Vec<Stroke>, join: bool) -> Vec<Stroke> {
|
||||
let mut out: Vec<Stroke> = Vec::new();
|
||||
let mut open: Vec<Stroke> = Vec::new();
|
||||
for s in strokes {
|
||||
if s.closed {
|
||||
if s.closed || (!join && !s.straight()) {
|
||||
out.push(s);
|
||||
} else {
|
||||
open.push(s);
|
||||
@@ -597,26 +675,32 @@ fn join_strokes(strokes: Vec<Stroke>) -> Vec<Stroke> {
|
||||
}) {
|
||||
let mut next = open.remove(i);
|
||||
if !same_point(next.points[0], end) {
|
||||
next.points.reverse();
|
||||
next.reverse();
|
||||
}
|
||||
chain.points.extend(next.points.into_iter().skip(1));
|
||||
chain.bulges.pop();
|
||||
chain.bulges.extend(next.bulges);
|
||||
} else if let Some(i) = open.iter().position(|o| {
|
||||
fits(o)
|
||||
&& (same_point(*o.points.last().unwrap(), start) || same_point(o.points[0], start))
|
||||
}) {
|
||||
let mut prev = open.remove(i);
|
||||
if !same_point(*prev.points.last().unwrap(), start) {
|
||||
prev.points.reverse();
|
||||
prev.reverse();
|
||||
}
|
||||
prev.points.pop();
|
||||
prev.points.extend(chain.points);
|
||||
chain.points = prev.points;
|
||||
prev.bulges.pop();
|
||||
prev.bulges.extend(chain.bulges);
|
||||
chain.bulges = prev.bulges;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if chain.points.len() > 3 && same_point(chain.points[0], *chain.points.last().unwrap()) {
|
||||
chain.points.pop();
|
||||
chain.bulges.pop();
|
||||
chain.closed = true;
|
||||
}
|
||||
out.push(chain);
|
||||
@@ -624,13 +708,45 @@ fn join_strokes(strokes: Vec<Stroke>) -> Vec<Stroke> {
|
||||
out
|
||||
}
|
||||
|
||||
/// An open stroke cut into its straight runs and one stroke per arc.
|
||||
fn split_at_arcs(stroke: Stroke) -> Vec<Stroke> {
|
||||
let mut out = Vec::new();
|
||||
let piece = |points: Vec<[f64; 2]>, bulges: Vec<f64>| Stroke {
|
||||
layer: stroke.layer.clone(),
|
||||
color: stroke.color,
|
||||
weight: stroke.weight,
|
||||
points,
|
||||
bulges,
|
||||
closed: false,
|
||||
};
|
||||
let mut run: Vec<[f64; 2]> = vec![stroke.points[0]];
|
||||
for i in 0..stroke.points.len() - 1 {
|
||||
let (a, b, bulge) = (stroke.points[i], stroke.points[i + 1], stroke.bulges[i]);
|
||||
if bulge == 0.0 {
|
||||
run.push(b);
|
||||
continue;
|
||||
}
|
||||
if run.len() > 1 {
|
||||
let n = run.len();
|
||||
out.push(piece(std::mem::take(&mut run), vec![0.0; n]));
|
||||
}
|
||||
out.push(piece(vec![a, b], vec![bulge, 0.0]));
|
||||
run = vec![b];
|
||||
}
|
||||
if run.len() > 1 {
|
||||
let n = run.len();
|
||||
out.push(piece(run, vec![0.0; n]));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Runs of three or more equal collinear dashes with equal gaps become one
|
||||
/// line from the first dash's start to the last's end, with the dash length
|
||||
/// (the linetype scale).
|
||||
// ponytail: dashes are matched along one direction at a time in drawing
|
||||
// order; patterns of mixed dash lengths stay separate lines.
|
||||
fn infer_dashes(strokes: Vec<Stroke>) -> Vec<(Stroke, Option<f64>)> {
|
||||
let is_dash = |s: &Stroke| !s.closed && s.points.len() == 2;
|
||||
let is_dash = |s: &Stroke| !s.closed && s.points.len() == 2 && s.straight();
|
||||
let len = |s: &Stroke| {
|
||||
let (a, b) = (s.points[0], s.points[1]);
|
||||
(b[0] - a[0]).hypot(b[1] - a[1])
|
||||
@@ -777,19 +893,36 @@ pub fn convert(
|
||||
(common.layer, common.color, common.line_weight) =
|
||||
(layer.clone(), color, weight);
|
||||
curves.push(circle);
|
||||
} else if sp.segments.iter().all(|s| matches!(s, Segment::Line(..))) {
|
||||
} else if let Some(mut bulges) = sp
|
||||
.segments
|
||||
.iter()
|
||||
.map(|s| match s {
|
||||
Segment::Line(..) => Some(0.0),
|
||||
cubic => arc_bulge(cubic),
|
||||
})
|
||||
.collect::<Option<Vec<f64>>>()
|
||||
{
|
||||
let mut points = vec![sp.segments[0].start()];
|
||||
points.extend(sp.segments.iter().map(|s| s.end()));
|
||||
if sp.closed && points.len() > 2 {
|
||||
points.pop();
|
||||
} else {
|
||||
bulges.push(0.0);
|
||||
}
|
||||
strokes.push(Stroke {
|
||||
let stroke = Stroke {
|
||||
layer: layer.clone(),
|
||||
color,
|
||||
weight,
|
||||
points,
|
||||
bulges,
|
||||
closed: sp.closed,
|
||||
});
|
||||
};
|
||||
if settings.join || stroke.straight() || stroke.closed {
|
||||
strokes.push(stroke);
|
||||
} else {
|
||||
// Join off: each arc apart, straight runs between them.
|
||||
strokes.extend(split_at_arcs(stroke));
|
||||
}
|
||||
} else {
|
||||
let mut spline = EntityType::Spline(spline_of(sp, &world3));
|
||||
let common = spline.common_mut();
|
||||
@@ -873,14 +1006,42 @@ pub fn convert(
|
||||
}
|
||||
|
||||
// The joined strokes, then (when asked) dashes read as one dashed line.
|
||||
let strokes = join_strokes(strokes);
|
||||
let strokes = join_strokes(strokes, settings.join);
|
||||
let strokes: Vec<(Stroke, Option<f64>)> = if settings.linetypes {
|
||||
infer_dashes(strokes)
|
||||
} else {
|
||||
strokes.into_iter().map(|s| (s, None)).collect()
|
||||
};
|
||||
// A mirrored placement turns arcs the other way.
|
||||
let turn = (underlay.x_scale * underlay.y_scale).signum();
|
||||
for (stroke, dash) in strokes {
|
||||
// A lone arc is an ARC.
|
||||
if !stroke.closed && stroke.points.len() == 2 && stroke.bulges[0] != 0.0 {
|
||||
let (a, b) = (world2(stroke.points[0]), world2(stroke.points[1]));
|
||||
let bulge = stroke.bulges[0] * turn;
|
||||
if let Some(ba) = crate::entities::common::BulgeArc::from_bulge([a.x, a.y], [b.x, b.y], bulge) {
|
||||
let (start, end) = if bulge > 0.0 {
|
||||
(ba.start_angle, ba.end_angle)
|
||||
} else {
|
||||
(ba.end_angle, ba.start_angle)
|
||||
};
|
||||
let tau = std::f64::consts::TAU;
|
||||
let mut arc = Arc::from_center_radius_angles(
|
||||
Vector3::new(ba.center[0], ba.center[1], z),
|
||||
ba.radius,
|
||||
start.rem_euclid(tau),
|
||||
end.rem_euclid(tau),
|
||||
);
|
||||
(arc.common.layer, arc.common.color, arc.common.line_weight) =
|
||||
(stroke.layer, stroke.color, stroke.weight);
|
||||
result.entities.push(EntityType::Arc(arc));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
let mut pl = LwPolyline::from_points(stroke.points.iter().map(|p| world2(*p)).collect());
|
||||
for (vertex, bulge) in pl.vertices.iter_mut().zip(&stroke.bulges) {
|
||||
vertex.bulge = bulge * turn;
|
||||
}
|
||||
pl.is_closed = stroke.closed;
|
||||
pl.elevation = z;
|
||||
(pl.common.layer, pl.common.color, pl.common.line_weight) =
|
||||
|
||||
@@ -1286,16 +1286,28 @@ fn tessellate_entity_inner(
|
||||
b.set_fixed_screen_width(2.0);
|
||||
}
|
||||
}
|
||||
// PDF underlay geometry: a hidden, unplotted wire that object snaps
|
||||
// (nearest, intersection, perpendicular) find, beside the page frame.
|
||||
// PDF underlay geometry: hidden, unplotted wires that object snaps
|
||||
// (nearest, intersection, perpendicular, centre) find, beside the page
|
||||
// frame — one per segment or circle, so they intersect each other.
|
||||
if let EntityType::Underlay(underlay) = e {
|
||||
let geometry = crate::scene::model::pdf_vector::underlay_snap_geometry(underlay, document);
|
||||
if !geometry.is_empty() {
|
||||
let (points, points_low) = convert::tessellate::points_to_ds(geometry);
|
||||
for piece in crate::scene::model::pdf_vector::underlay_snap_geometry(underlay, document) {
|
||||
let (points, points_low) = convert::tessellate::points_to_ds(piece.points);
|
||||
let mut wire = WireModel::solid(h.value().to_string(), points, entity_color, sel);
|
||||
wire.points_low = points_low;
|
||||
wire.display_visible = false;
|
||||
wire.plot_visible = false;
|
||||
if let Some((center, radius)) = piece.circle {
|
||||
wire.tangent_geoms = vec![crate::scene::model::wire_model::TangentGeom::PlanarCircle {
|
||||
center,
|
||||
axis_x: [1.0, 0.0, 0.0],
|
||||
axis_y: [0.0, 1.0, 0.0],
|
||||
radius,
|
||||
}];
|
||||
wire.snap_pts = vec![(
|
||||
glam::DVec3::from(center),
|
||||
crate::scene::model::wire_model::SnapHint::Center,
|
||||
)];
|
||||
}
|
||||
set_wire_aabb(&mut wire, aabb);
|
||||
bases.push(wire);
|
||||
}
|
||||
|
||||
@@ -221,7 +221,11 @@ impl ImageModel {
|
||||
// Hidden PDF layers come from the underlay's layer overrides.
|
||||
let source = super::pdf_layers::underlay_source(u, &def.file_path);
|
||||
let raster = pdf_raster::rasterize_page_display(&source, page)?;
|
||||
let bg_lum = 0.299 * background[0] + 0.587 * background[1] + 0.114 * background[2];
|
||||
// Dark means an HSL lightness under one half: pure blue counts as
|
||||
// light, (0, 128, 0) as dark.
|
||||
let bg_max = background[0].max(background[1]).max(background[2]);
|
||||
let bg_min = background[0].min(background[1]).min(background[2]);
|
||||
let bg_lum = (bg_max + bg_min) / 2.0;
|
||||
let pixels = pdf_raster::adjusted_pixels(
|
||||
&source,
|
||||
page,
|
||||
@@ -230,7 +234,7 @@ impl ImageModel {
|
||||
contrast: u.contrast.min(100),
|
||||
monochrome: u.flags.contains(UnderlayDisplayFlags::MONOCHROME),
|
||||
adjust_for_background: u.flags.contains(UnderlayDisplayFlags::ADJUST_FOR_BACKGROUND),
|
||||
dark_background: bg_lum <= 0.5,
|
||||
dark_background: bg_lum < 0.5,
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -252,8 +252,6 @@ pub fn adjusted_pixels(path: &str, page: &str, raster: &PdfPage, adjust: PageAdj
|
||||
// (plus 20/255, capped at full), hue and saturation kept: black
|
||||
// becomes white, dark red light pink. On a light background the
|
||||
// colours stay as drawn.
|
||||
// ponytail: offset and the light-background rule measured on the
|
||||
// default backgrounds only (pure white on paper not measured).
|
||||
let (h, s, l) = rgb_to_hsl(rgb);
|
||||
rgb = hsl_to_rgb(h, s, (1.0 - l + 20.0 / 255.0).min(1.0));
|
||||
}
|
||||
|
||||
@@ -89,6 +89,16 @@ pub struct PageVectors {
|
||||
pub texts: Vec<PdfText>,
|
||||
/// Filled only when images are asked for (`page_content`).
|
||||
pub images: Vec<PdfImage>,
|
||||
/// Paths, texts and images in the order the page draws them.
|
||||
pub order: Vec<Drawn>,
|
||||
}
|
||||
|
||||
/// One drawn item: an index into `paths`, `texts` or `images`.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Drawn {
|
||||
Path(usize),
|
||||
Text(usize),
|
||||
Image(usize),
|
||||
}
|
||||
|
||||
type Key = (String, String);
|
||||
@@ -168,10 +178,54 @@ pub fn page_content_by_layer(
|
||||
only.images.retain(|i| !base.images.contains(i));
|
||||
out.push((Some(layer.name.clone()), only));
|
||||
}
|
||||
let mut base = base;
|
||||
images_to_last_layer(path, page, &mut base, &mut out);
|
||||
out.insert(0, (None, base));
|
||||
Some(out)
|
||||
}
|
||||
|
||||
/// An image that no layer holds goes with the layer drawn last before it
|
||||
/// (with none before it, it stays unlayered): the page is read with every
|
||||
/// layer on for the drawing order.
|
||||
fn images_to_last_layer(
|
||||
path: &str,
|
||||
page: &str,
|
||||
base: &mut PageVectors,
|
||||
layered: &mut [(Option<String>, PageVectors)],
|
||||
) {
|
||||
if base.images.is_empty() || layered.is_empty() {
|
||||
return;
|
||||
}
|
||||
let Some(full) = page_content(&super::pdf_layers::source_with_layers(path, &[]), page) else {
|
||||
return;
|
||||
};
|
||||
let key = |s: &dyn std::fmt::Debug| format!("{s:?}");
|
||||
let owner = |item: String| {
|
||||
layered.iter().position(|(_, pv)| {
|
||||
pv.paths.iter().any(|p| key(p) == item) || pv.texts.iter().any(|t| key(t) == item)
|
||||
})
|
||||
};
|
||||
let mut last: Option<usize> = None;
|
||||
let mut moves: Vec<(usize, PdfImage)> = Vec::new();
|
||||
for drawn in &full.order {
|
||||
match *drawn {
|
||||
Drawn::Path(i) => last = owner(key(&full.paths[i])).or(last),
|
||||
Drawn::Text(i) => last = owner(key(&full.texts[i])).or(last),
|
||||
Drawn::Image(i) => {
|
||||
let image = &full.images[i];
|
||||
if let Some(n) = layered.iter().position(|(_, pv)| pv.images.contains(image)) {
|
||||
last = Some(n);
|
||||
} else if let (Some(n), Some(at)) = (last, base.images.iter().position(|b| b == image)) {
|
||||
moves.push((n, base.images.remove(at)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (n, image) in moves {
|
||||
layered[n].1.images.push(image);
|
||||
}
|
||||
}
|
||||
|
||||
fn read_page(path: &str, page: &str) -> Option<PageVectors> {
|
||||
read_page_with(path, page, false)
|
||||
}
|
||||
@@ -229,6 +283,7 @@ fn read_page_with(path: &str, page: &str, images: bool) -> Option<PageVectors> {
|
||||
paths: device.paths,
|
||||
texts: texts.into_iter().map(|t| t.text).collect(),
|
||||
images: device.images,
|
||||
order: device.order,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -250,12 +305,14 @@ struct Collector {
|
||||
font_order: Vec<u128>,
|
||||
want_images: bool,
|
||||
images: Vec<PdfImage>,
|
||||
order: Vec<Drawn>,
|
||||
}
|
||||
|
||||
impl Collector {
|
||||
fn finish_text(&mut self) {
|
||||
if let Some(text) = self.current.take() {
|
||||
if !text.text.text.trim().is_empty() {
|
||||
self.order.push(Drawn::Text(self.texts.len()));
|
||||
self.texts.push(text);
|
||||
}
|
||||
}
|
||||
@@ -351,6 +408,7 @@ impl<'a> Device<'a> for Collector {
|
||||
}
|
||||
PathDrawMode::Fill(_) => (None, Some(rgb(paint))),
|
||||
};
|
||||
self.order.push(Drawn::Path(self.paths.len()));
|
||||
self.paths.push(PdfPath { subpaths, stroke, fill });
|
||||
}
|
||||
fn push_clip_path(&mut self, _: &ClipPath) {}
|
||||
@@ -482,7 +540,10 @@ impl<'a> Device<'a> for Collector {
|
||||
},
|
||||
None,
|
||||
);
|
||||
self.images.extend(decoded);
|
||||
if let Some(image) = decoded {
|
||||
self.order.push(Drawn::Image(self.images.len()));
|
||||
self.images.push(image);
|
||||
}
|
||||
}
|
||||
fn pop_clip_path(&mut self) {}
|
||||
fn pop_transparency_group(&mut self) {}
|
||||
@@ -605,13 +666,21 @@ pub fn underlay_snap_points(
|
||||
// ponytail: flat cap; a spatial index over page segments for huge drawings.
|
||||
const MAX_GEOMETRY_POINTS: usize = 200_000;
|
||||
|
||||
/// The underlay's PDF geometry as world polylines (NaN-separated, curves
|
||||
/// flattened) for nearest, intersection and perpendicular snaps. Empty when
|
||||
/// One piece of an underlay's PDF geometry for snapping: a world polyline
|
||||
/// (curves flattened), and for a circle its centre and radius.
|
||||
pub struct SnapPiece {
|
||||
pub points: Vec<[f64; 3]>,
|
||||
pub circle: Option<([f64; 3], f64)>,
|
||||
}
|
||||
|
||||
/// The underlay's PDF geometry for nearest, intersection, perpendicular and
|
||||
/// centre snaps: each straight segment, each curve and each circle its own
|
||||
/// piece, so pieces cross each other as separate objects do. Empty when
|
||||
/// PDFOSNAP is off or the page is not shown.
|
||||
pub fn underlay_snap_geometry(
|
||||
u: &codec::entities::Underlay,
|
||||
document: &codec::CadDocument,
|
||||
) -> Vec<[f64; 3]> {
|
||||
) -> Vec<SnapPiece> {
|
||||
if !pdf_osnap() || !u.flags.contains(codec::entities::UnderlayDisplayFlags::ON) {
|
||||
return Vec::new();
|
||||
}
|
||||
@@ -626,21 +695,35 @@ pub fn underlay_snap_geometry(
|
||||
return Vec::new();
|
||||
};
|
||||
let world = |p: [f64; 2]| crate::entities::underlay::local_to_world(u, p);
|
||||
let mut out: Vec<[f64; 3]> = Vec::new();
|
||||
// A circle stays one only while the placement scales both axes alike.
|
||||
let uniform = (u.x_scale.abs() - u.y_scale.abs()).abs() <= 1e-9 * u.x_scale.abs().max(1e-12);
|
||||
let flatten = |seg: &Segment, points: &mut Vec<[f64; 3]>| {
|
||||
let steps = if matches!(seg, Segment::Cubic(..)) { 12 } else { 1 };
|
||||
for k in 1..=steps {
|
||||
points.push(world(bezier(seg, k as f64 / steps as f64)));
|
||||
}
|
||||
};
|
||||
let mut out: Vec<SnapPiece> = Vec::new();
|
||||
let mut total = 0usize;
|
||||
for path in &vectors.paths {
|
||||
for sp in &path.subpaths {
|
||||
if out.len() >= MAX_GEOMETRY_POINTS {
|
||||
if total >= MAX_GEOMETRY_POINTS {
|
||||
return out;
|
||||
}
|
||||
if !out.is_empty() {
|
||||
out.push([f64::NAN; 3]);
|
||||
}
|
||||
out.push(world(sp.segments[0].start()));
|
||||
for seg in &sp.segments {
|
||||
let steps = if matches!(seg, Segment::Cubic(..)) { 12 } else { 1 };
|
||||
for k in 1..=steps {
|
||||
out.push(world(bezier(seg, k as f64 / steps as f64)));
|
||||
if let (Some((c, r)), true) = (circle_of(sp), uniform) {
|
||||
let mut points = vec![world(sp.segments[0].start())];
|
||||
for seg in &sp.segments {
|
||||
flatten(seg, &mut points);
|
||||
}
|
||||
total += points.len();
|
||||
out.push(SnapPiece { points, circle: Some((world(c), r * u.x_scale.abs())) });
|
||||
continue;
|
||||
}
|
||||
for seg in &sp.segments {
|
||||
let mut points = vec![world(seg.start())];
|
||||
flatten(seg, &mut points);
|
||||
total += points.len();
|
||||
out.push(SnapPiece { points, circle: None });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user