forked from 1b-agent/opencadstudio-upstream
first commit
This commit is contained in:
1 parent
d52c6440ff
commit
d2fdac7f1e
274 files changed
+89330
No files matched your search
@@ -0,0 +1,65 @@
|
||||
// GroupCommand — text-input phase after objects are selected.
|
||||
//
|
||||
// Receives the pre-selected handles from dispatch_command and asks the user
|
||||
// for a group name. Pressing Enter without typing uses the auto-generated name.
|
||||
// Result: CmdResult::CreateGroup { handles, name }
|
||||
|
||||
use acadrust::Handle;
|
||||
use glam::Vec3;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::scene::wire_model::WireModel;
|
||||
|
||||
pub struct GroupCommand {
|
||||
handles: Vec<Handle>,
|
||||
/// Auto-generated fallback name shown in the prompt.
|
||||
auto_name: String,
|
||||
}
|
||||
|
||||
impl GroupCommand {
|
||||
pub fn new(handles: Vec<Handle>, auto_name: String) -> Self {
|
||||
Self { handles, auto_name }
|
||||
}
|
||||
}
|
||||
|
||||
impl CadCommand for GroupCommand {
|
||||
fn name(&self) -> &'static str {
|
||||
"GROUP"
|
||||
}
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
format!("GROUP Enter group name [{}]:", self.auto_name)
|
||||
}
|
||||
|
||||
fn wants_text_input(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn on_text_input(&mut self, text: &str) -> Option<CmdResult> {
|
||||
let name = if text.trim().is_empty() {
|
||||
std::mem::take(&mut self.auto_name)
|
||||
} else {
|
||||
text.trim().to_string()
|
||||
};
|
||||
Some(CmdResult::CreateGroup {
|
||||
handles: std::mem::take(&mut self.handles),
|
||||
name,
|
||||
})
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
// Enter with no typed name uses the auto-generated name.
|
||||
CmdResult::CreateGroup {
|
||||
handles: std::mem::take(&mut self.handles),
|
||||
name: std::mem::take(&mut self.auto_name),
|
||||
}
|
||||
}
|
||||
|
||||
fn on_point(&mut self, _pt: Vec3) -> CmdResult {
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
|
||||
fn on_hover_entity(&mut self, _handle: Handle, _pt: Vec3) -> Vec<WireModel> {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
// LayMatchCommand — two-phase layer match.
|
||||
//
|
||||
// Phase 1: user selects destination objects (whose layer will be changed).
|
||||
// Phase 2: user selects a single source object (whose layer will be applied).
|
||||
|
||||
use acadrust::Handle;
|
||||
use glam::Vec3;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::scene::wire_model::WireModel;
|
||||
|
||||
pub struct LayMatchCommand {
|
||||
dest_handles: Vec<Handle>,
|
||||
}
|
||||
|
||||
impl LayMatchCommand {
|
||||
pub fn new(dest: Vec<Handle>) -> Self {
|
||||
Self { dest_handles: dest }
|
||||
}
|
||||
}
|
||||
|
||||
impl CadCommand for LayMatchCommand {
|
||||
fn name(&self) -> &'static str {
|
||||
"LAYMATCH"
|
||||
}
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
if self.dest_handles.is_empty() {
|
||||
"LAYMATCH Select objects to change layer:".into()
|
||||
} else {
|
||||
"LAYMATCH Select source object to match layer from:".into()
|
||||
}
|
||||
}
|
||||
|
||||
fn is_selection_gathering(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn on_selection_complete(&mut self, handles: Vec<Handle>) -> CmdResult {
|
||||
if handles.is_empty() {
|
||||
return CmdResult::NeedPoint;
|
||||
}
|
||||
if self.dest_handles.is_empty() {
|
||||
// Phase 1 complete — store destinations, move to phase 2.
|
||||
self.dest_handles = handles;
|
||||
CmdResult::NeedPoint
|
||||
} else {
|
||||
// Phase 2 complete — first handle is the source object.
|
||||
let src = handles[0];
|
||||
CmdResult::MatchEntityLayer {
|
||||
dest: std::mem::take(&mut self.dest_handles),
|
||||
src,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn on_point(&mut self, _pt: Vec3) -> CmdResult {
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
fn on_escape(&mut self) -> CmdResult {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
fn on_hover_entity(&mut self, _handle: Handle, _pt: Vec3) -> Vec<WireModel> {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
// MatchPropCommand — two-phase match properties.
|
||||
//
|
||||
// Phase 1: user picks the SOURCE object (single entity pick).
|
||||
// Phase 2: user selects DESTINATION objects (multi-select, Enter to finish).
|
||||
// Result: CmdResult::MatchProperties { dest, src }
|
||||
|
||||
use acadrust::Handle;
|
||||
use glam::Vec3;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::scene::wire_model::WireModel;
|
||||
|
||||
pub struct MatchPropCommand {
|
||||
/// Source handle; NULL until phase 1 is complete.
|
||||
src: Handle,
|
||||
}
|
||||
|
||||
impl MatchPropCommand {
|
||||
pub fn new() -> Self {
|
||||
Self { src: Handle::NULL }
|
||||
}
|
||||
|
||||
fn phase1_done(&self) -> bool {
|
||||
!self.src.is_null()
|
||||
}
|
||||
}
|
||||
|
||||
impl CadCommand for MatchPropCommand {
|
||||
fn name(&self) -> &'static str {
|
||||
"MATCHPROP"
|
||||
}
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
if !self.phase1_done() {
|
||||
"MATCHPROP Select source object:".into()
|
||||
} else {
|
||||
"MATCHPROP Select destination objects (Enter to apply):".into()
|
||||
}
|
||||
}
|
||||
|
||||
// ── Phase 1: single entity pick ───────────────────────────────────────
|
||||
|
||||
fn needs_entity_pick(&self) -> bool {
|
||||
!self.phase1_done()
|
||||
}
|
||||
|
||||
fn on_entity_pick(&mut self, handle: Handle, _pt: Vec3) -> CmdResult {
|
||||
if handle.is_null() {
|
||||
return CmdResult::NeedPoint; // nothing hit, keep waiting
|
||||
}
|
||||
self.src = handle;
|
||||
CmdResult::NeedPoint // move to phase 2 (selection gathering)
|
||||
}
|
||||
|
||||
// ── Phase 2: destination selection gathering ──────────────────────────
|
||||
|
||||
fn is_selection_gathering(&self) -> bool {
|
||||
self.phase1_done()
|
||||
}
|
||||
|
||||
fn on_selection_complete(&mut self, handles: Vec<Handle>) -> CmdResult {
|
||||
if handles.is_empty() {
|
||||
return CmdResult::NeedPoint;
|
||||
}
|
||||
CmdResult::MatchProperties {
|
||||
dest: handles,
|
||||
src: self.src,
|
||||
}
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
|
||||
fn on_point(&mut self, _pt: Vec3) -> CmdResult {
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
|
||||
fn on_hover_entity(&mut self, _handle: Handle, _pt: Vec3) -> Vec<WireModel> {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
// Command system — trait for interactive CAD commands.
|
||||
//
|
||||
// Each tool that requires user interaction (point picks, object selection,
|
||||
// numeric input) implements `CadCommand`. The active command receives
|
||||
// viewport events from main.rs and returns `CmdResult` tokens that tell
|
||||
// the host what to do next.
|
||||
|
||||
pub mod group;
|
||||
pub mod laymatch;
|
||||
pub mod matchprop;
|
||||
pub mod paste;
|
||||
pub mod select;
|
||||
pub mod ungroup;
|
||||
|
||||
use crate::scene::hatch_model::HatchModel;
|
||||
use crate::scene::wire_model::WireModel;
|
||||
use acadrust::{EntityType, Handle};
|
||||
use glam::Vec3;
|
||||
|
||||
// ── Transform ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// A geometric transformation applied to existing entities.
|
||||
#[derive(Clone)]
|
||||
pub enum EntityTransform {
|
||||
/// Move every point by the given world-space delta (Y-up XZ plane).
|
||||
Translate(Vec3),
|
||||
/// Rotate around `center` by `angle_rad` in the XZ plane (Y-up).
|
||||
Rotate { center: Vec3, angle_rad: f32 },
|
||||
/// Uniform scale from `center` by `factor`.
|
||||
Scale { center: Vec3, factor: f32 },
|
||||
/// Mirror across the line through `p1`→`p2` in the XZ plane (Y-up).
|
||||
Mirror { p1: Vec3, p2: Vec3 },
|
||||
}
|
||||
|
||||
// ── Tangent object ─────────────────────────────────────────────────────────
|
||||
|
||||
/// Geometric representation of a tangent-snap target.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum TangentObject {
|
||||
/// Infinite line through two world-space XZ-plane points.
|
||||
Line { p1: Vec3, p2: Vec3 },
|
||||
/// Circle in the XZ plane.
|
||||
Circle { center: Vec3, radius: f32 },
|
||||
}
|
||||
|
||||
// ── Result token ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Returned by every `CadCommand` method to tell main.rs what to do.
|
||||
#[allow(dead_code)]
|
||||
pub enum CmdResult {
|
||||
/// Command is still waiting for the next point; show updated prompt.
|
||||
NeedPoint,
|
||||
/// Update the committed-segment wire (normal colour) and keep collecting points.
|
||||
InterimWire(WireModel),
|
||||
/// Update the in-progress (cyan) preview wire in the viewport.
|
||||
Preview(WireModel),
|
||||
/// Commit an acadrust entity to the document; keep the command active.
|
||||
CommitEntity(EntityType),
|
||||
/// Commit an acadrust entity to the document and end the command.
|
||||
CommitAndExit(EntityType),
|
||||
/// Create a block definition from existing entities and insert one reference.
|
||||
CreateBlock {
|
||||
handles: Vec<Handle>,
|
||||
name: String,
|
||||
base: Vec3,
|
||||
},
|
||||
/// Apply a transform to selected entities and end the command.
|
||||
TransformSelected(Vec<Handle>, EntityTransform),
|
||||
/// Copy selected entities with a transform; command stays active for more copies.
|
||||
CopySelected(Vec<Handle>, EntityTransform),
|
||||
/// Commit a hatch fill (stored in Scene::hatches, not the DXF document).
|
||||
CommitHatch(HatchModel),
|
||||
/// Copy selected entities with multiple transforms (e.g. rectangular array); end command.
|
||||
BatchCopy(Vec<Handle>, Vec<EntityTransform>),
|
||||
/// Erase `handle` and replace with new entities; command stays active.
|
||||
ReplaceEntity(Handle, Vec<EntityType>),
|
||||
/// Replace / delete multiple entities and add new ones; command ends.
|
||||
/// Each pair: (handle_to_erase, replacement_entities) — empty vec = delete only.
|
||||
ReplaceMany(Vec<(Handle, Vec<EntityType>)>, Vec<EntityType>),
|
||||
/// Cancel: discard any preview and end the command.
|
||||
Cancel,
|
||||
/// End the selection-gather phase and re-dispatch the named command
|
||||
/// with the gathered handles installed as the active scene selection.
|
||||
Relaunch(String, Vec<Handle>),
|
||||
/// Move `dest` entities to the layer of the `src` entity; end command.
|
||||
MatchEntityLayer { dest: Vec<Handle>, src: Handle },
|
||||
/// Copy all visual properties (layer/color/linetype/lineweight) from `src` to `dest`; end command.
|
||||
MatchProperties { dest: Vec<Handle>, src: Handle },
|
||||
/// Create a named group from the given entity handles; end command.
|
||||
CreateGroup { handles: Vec<Handle>, name: String },
|
||||
/// Dissolve all groups that contain any of the given handles; end command.
|
||||
DeleteGroups { handles: Vec<Handle> },
|
||||
/// Paste clipboard entities translated so their centroid lands at `base_pt`; end command.
|
||||
PasteClipboard { base_pt: Vec3 },
|
||||
}
|
||||
|
||||
// ── Trait ─────────────────────────────────────────────────────────────────
|
||||
|
||||
/// An interactive CAD command that collects user input step-by-step.
|
||||
pub trait CadCommand: Send {
|
||||
/// Short name shown in the command line prompt, e.g. `"LINE"`.
|
||||
#[allow(dead_code)]
|
||||
fn name(&self) -> &'static str;
|
||||
/// Current prompt string to display in the command line.
|
||||
fn prompt(&self) -> String;
|
||||
|
||||
/// Called when the user left-clicks in the viewport (point pick).
|
||||
fn on_point(&mut self, pt: Vec3) -> CmdResult;
|
||||
|
||||
/// Called when the user presses Enter (finalize / next option).
|
||||
fn on_enter(&mut self) -> CmdResult;
|
||||
|
||||
/// Called when the user presses Escape (cancel).
|
||||
#[allow(dead_code)]
|
||||
fn on_escape(&mut self) -> CmdResult {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
|
||||
/// Returns `true` when the command needs entity picking (hit-test) instead of point picking.
|
||||
fn needs_entity_pick(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Called when the user clicks and `needs_entity_pick()` is true.
|
||||
/// `handle` is the nearest wire's entity handle (Handle::NULL if nothing found).
|
||||
fn on_entity_pick(&mut self, _handle: Handle, _pt: Vec3) -> CmdResult {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
|
||||
/// Called after `CmdResult::ReplaceEntity` is applied to the document.
|
||||
/// `old` is the erased handle; `new_handles` are the handles assigned to the replacement entities.
|
||||
/// Commands that stay active across replaces should update their internal snapshots here.
|
||||
fn on_entity_replaced(&mut self, _old: Handle, _new_handles: &[Handle]) {}
|
||||
|
||||
/// Called on every mouse-move when `needs_entity_pick()` is true.
|
||||
/// Return preview wires showing the operation result under the cursor.
|
||||
/// Default: empty (no preview).
|
||||
fn on_hover_entity(&mut self, _handle: Handle, _pt: Vec3) -> Vec<WireModel> {
|
||||
vec![]
|
||||
}
|
||||
|
||||
/// Called on every mouse-move in the viewport.
|
||||
/// Return `Some(WireModel)` to update the rubber-band preview, `None` to skip.
|
||||
fn on_mouse_move(&mut self, _pt: Vec3) -> Option<WireModel> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Called on every mouse-move; return all preview wires to show (object ghosts + rubber-band).
|
||||
/// Default: forwards to `on_mouse_move` for backwards compatibility.
|
||||
fn on_preview_wires(&mut self, pt: Vec3) -> Vec<WireModel> {
|
||||
self.on_mouse_move(pt).into_iter().collect()
|
||||
}
|
||||
|
||||
/// Returns `true` when the command is waiting for text typed in the command line.
|
||||
fn wants_text_input(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Called when the user submits text via the command line while `wants_text_input` is true.
|
||||
fn on_text_input(&mut self, _text: &str) -> Option<CmdResult> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Returns `true` when the command is in a selection-gathering phase.
|
||||
/// While true, viewport clicks are routed through the normal selection
|
||||
/// system (single / box / polygon) instead of the command's point-pick path.
|
||||
/// After each completed selection action the host calls `on_selection_complete`.
|
||||
fn is_selection_gathering(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Called after a selection action completes while `is_selection_gathering` is true.
|
||||
/// `handles` is the full set of currently selected entities.
|
||||
/// Return `Relaunch` to fire the pending command, or `NeedPoint` to keep gathering.
|
||||
fn on_selection_complete(&mut self, _handles: Vec<Handle>) -> CmdResult {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
|
||||
/// Returns `true` when the command wants object picks via Tangent snap.
|
||||
fn needs_tangent_pick(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Called instead of `on_point` when the command needs a tangent pick
|
||||
/// and the snap system found a tangent object.
|
||||
fn on_tangent_point(&mut self, obj: TangentObject, hit: Vec3) -> CmdResult {
|
||||
let _ = obj;
|
||||
self.on_point(hit)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// PasteCommand — picks one insertion point then fires CmdResult::PasteClipboard.
|
||||
//
|
||||
// Holds pre-computed wire previews (from clipboard entities) and the centroid
|
||||
// so it can show a translated rubber-band preview while the user moves the mouse.
|
||||
|
||||
use acadrust::Handle;
|
||||
use glam::Vec3;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::scene::wire_model::WireModel;
|
||||
|
||||
pub struct PasteCommand {
|
||||
/// Wire models of the clipboard entities (used for preview).
|
||||
wires: Vec<WireModel>,
|
||||
/// Centroid of the clipboard entities (offset origin for translation).
|
||||
centroid: Vec3,
|
||||
}
|
||||
|
||||
impl PasteCommand {
|
||||
pub fn new(wires: Vec<WireModel>, centroid: Vec3) -> Self {
|
||||
Self { wires, centroid }
|
||||
}
|
||||
}
|
||||
|
||||
impl CadCommand for PasteCommand {
|
||||
fn name(&self) -> &'static str {
|
||||
"PASTECLIP"
|
||||
}
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
"PASTECLIP Pick insertion point:".into()
|
||||
}
|
||||
|
||||
fn on_point(&mut self, pt: Vec3) -> CmdResult {
|
||||
CmdResult::PasteClipboard { base_pt: pt }
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
|
||||
fn on_hover_entity(&mut self, _handle: Handle, _pt: Vec3) -> Vec<WireModel> {
|
||||
vec![]
|
||||
}
|
||||
|
||||
fn on_preview_wires(&mut self, pt: Vec3) -> Vec<WireModel> {
|
||||
let delta = pt - self.centroid;
|
||||
self.wires.iter().map(|w| w.translated(delta)).collect()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
// SelectObjectsCommand — generic "select objects then run command" gather phase.
|
||||
//
|
||||
// Used when a modify command is invoked with nothing pre-selected.
|
||||
// The user may single-click, box-select, or polygon-select any number of objects.
|
||||
// After the FIRST completed selection action (regardless of method) the command fires
|
||||
// immediately — no Enter required.
|
||||
|
||||
use acadrust::Handle;
|
||||
use glam::Vec3;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::scene::wire_model::WireModel;
|
||||
|
||||
pub struct SelectObjectsCommand {
|
||||
pending_cmd: String,
|
||||
}
|
||||
|
||||
impl SelectObjectsCommand {
|
||||
pub fn new(pending_cmd: &str) -> Self {
|
||||
Self {
|
||||
pending_cmd: pending_cmd.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CadCommand for SelectObjectsCommand {
|
||||
fn name(&self) -> &'static str {
|
||||
"SELECT"
|
||||
}
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
format!("{} Select objects:", self.pending_cmd)
|
||||
}
|
||||
|
||||
// Opt into the selection-gathering path; host routes clicks through
|
||||
// the normal selection system and calls on_selection_complete after each action.
|
||||
fn is_selection_gathering(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn on_selection_complete(&mut self, handles: Vec<Handle>) -> CmdResult {
|
||||
if handles.is_empty() {
|
||||
return CmdResult::NeedPoint;
|
||||
}
|
||||
CmdResult::Relaunch(std::mem::take(&mut self.pending_cmd), handles)
|
||||
}
|
||||
|
||||
// These are never called while is_selection_gathering is true, but the
|
||||
// trait requires implementations.
|
||||
fn on_point(&mut self, _pt: Vec3) -> CmdResult {
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
fn on_escape(&mut self) -> CmdResult {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
fn on_hover_entity(&mut self, _handle: Handle, _pt: Vec3) -> Vec<WireModel> {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// UngroupCommand — selection gathering then dissolve all groups that
|
||||
// contain any of the selected handles.
|
||||
// Result: CmdResult::DeleteGroups { handles }
|
||||
|
||||
use acadrust::Handle;
|
||||
use glam::Vec3;
|
||||
|
||||
use crate::command::{CadCommand, CmdResult};
|
||||
use crate::scene::wire_model::WireModel;
|
||||
|
||||
pub struct UngroupCommand;
|
||||
|
||||
impl UngroupCommand {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
impl CadCommand for UngroupCommand {
|
||||
fn name(&self) -> &'static str {
|
||||
"UNGROUP"
|
||||
}
|
||||
|
||||
fn prompt(&self) -> String {
|
||||
"UNGROUP Select grouped objects:".into()
|
||||
}
|
||||
|
||||
fn is_selection_gathering(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn on_selection_complete(&mut self, handles: Vec<Handle>) -> CmdResult {
|
||||
if handles.is_empty() {
|
||||
return CmdResult::NeedPoint;
|
||||
}
|
||||
CmdResult::DeleteGroups { handles }
|
||||
}
|
||||
|
||||
fn on_point(&mut self, _pt: Vec3) -> CmdResult {
|
||||
CmdResult::NeedPoint
|
||||
}
|
||||
|
||||
fn on_enter(&mut self) -> CmdResult {
|
||||
CmdResult::Cancel
|
||||
}
|
||||
|
||||
fn on_hover_entity(&mut self, _handle: Handle, _pt: Vec3) -> Vec<WireModel> {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user