cdrsdk 0.2.0

Rust SDK for CorelDRAW automation via COM IDispatch late binding
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//! `IVGSnapPoint` 涓€鏃?鈥斺€?鎶撳彇鐐?
//!
//! - [`IvgSnapPoint`]          : 閫氱敤鎶撳彇鐐?
//! - [`IvgSnapPoints`]         : 闆嗗悎
//! - [`IvgSnapPointRange`]     : 鑼冨洿
//! - [`IvgUserSnapPoint`]      : 鐢ㄦ埛鑷畾涔夋姄鍙栫偣
//! - [`IvgObjectSnapPoint`]    : 瀵硅薄鎶撳彇鐐癸紙涓偣銆佺鐐广€佸渾蹇冣€︹€︼級
//! - [`IvgBBoxSnapPoint`]      : 鍖呭洿鐩掕鐐?
//! - [`IvgEdgeSnapPoint`]      : 杈圭紭鎶撳彇鐐?

use wincom::{ComObject, Variant};
use windows::Win32::System::Com::IDispatch;

use crate::curve::IvgNode;
use crate::shape::IvgShape;

// =============================================================
// IvgSnapPoint
// =============================================================

pub struct IvgSnapPoint {
    disp: ComObject,
}

impl IvgSnapPoint {
    pub fn new(disp: IDispatch) -> Self {
        Self { disp: ComObject::from_dispatch(disp, crate::types::IID_IDISPATCH) }
    }

    pub fn from_disp(disp: IDispatch) -> Self {
        Self::new(disp)
    }

    pub fn as_variant(&self) -> Variant {
        self.disp.as_variant()
    }

    fn prop_i64(&self, name: &str) -> Option<i64> {
        self.disp.get_property(name).ok()?.to_i64().ok()
    }

    fn prop_f64(&self, name: &str) -> Option<f64> {
        self.disp.get_property(name).ok()?.to_f64().ok()
    }

    fn prop_bool(&self, name: &str) -> Option<bool> {
        self.disp.get_property(name).ok()?.to_bool().ok()
    }

    fn prop_string(&self, name: &str) -> Option<String> {
        self.disp.get_property(name).ok()?.to_string().ok()
    }

    fn prop_dispatch(&self, name: &str) -> Option<IDispatch> {
        self.disp.get_property(name).ok()?.to_idispatch().ok()
    }

    fn put_f64(&self, name: &str, v: f64) -> bool {
        let arg = Variant::from_f64(v);
        self.disp.set_property(name, vec![arg]).is_ok()
    }

    fn put_bool(&self, name: &str, v: bool) -> bool {
        let arg = Variant::from_bool(v);
        self.disp.set_property(name, vec![arg]).is_ok()
    }

    // ---- 浣嶇疆 ----

    pub fn position_x(&self) -> Option<f64> { self.prop_f64("PositionX") }
    pub fn set_position_x(&self, v: f64) -> bool { self.put_f64("PositionX", v) }

    pub fn position_y(&self) -> Option<f64> { self.prop_f64("PositionY") }
    pub fn set_position_y(&self, v: f64) -> bool { self.put_f64("PositionY", v) }

    pub fn get_position(&self) -> Option<(f64, f64)> {
        Some((self.position_x()?, self.position_y()?))
    }

    pub fn set_position(&self, x: f64, y: f64) -> bool {
        self.set_position_x(x) && self.set_position_y(y)
    }

    pub fn move_by(&self, dx: f64, dy: f64) -> bool {
        let args = vec![
            Variant::from_f64(dx),
            Variant::from_f64(dy),
        ];
        self.disp.invoke_method("Move", args).is_ok()
    }

    // ---- 绫诲瀷 / 鏂瑰悜 ----

    /// `cdrPointType`
    pub fn point_type(&self) -> Option<i64> { self.prop_i64("Type") }

    pub fn direction(&self) -> Option<f64> { self.prop_f64("Direction") }
    pub fn set_direction(&self, v: f64) -> bool { self.put_f64("Direction", v) }

    pub fn uses_direction(&self) -> Option<bool> { self.prop_bool("UsesDirection") }
    pub fn set_uses_direction(&self, v: bool) -> bool { self.put_bool("UsesDirection", v) }

    pub fn is_selectable(&self) -> Option<bool> { self.prop_bool("IsSelectable") }
    pub fn is_deletable(&self) -> Option<bool> { self.prop_bool("IsDeletable") }
    pub fn is_movable(&self) -> Option<bool> { self.prop_bool("IsMovable") }
    pub fn can_change_direction(&self) -> Option<bool> {
        self.prop_bool("CanChangeDirection")
    }

    pub fn selected(&self) -> Option<bool> { self.prop_bool("Selected") }
    pub fn set_selected(&self, v: bool) -> bool { self.put_bool("Selected", v) }

    pub fn create_selection(&self) -> bool {
        self.disp.invoke_method("CreateSelection", vec![]).is_ok()
    }

    // ---- 鍏宠仈 ----

    pub fn node(&self) -> Option<IvgNode> {
        self.prop_dispatch("Node").map(IvgNode::new)
    }

    pub fn shape(&self) -> Option<IvgShape> {
        self.prop_dispatch("Shape").map(IvgShape::new)
    }

    /// 寮曠敤鏁版嵁锛圙UID 瀛楃涓诧紝鍞竴瀹氫綅涓€涓姄鍙栫偣锛夈€?
    pub fn reference_data(&self) -> Option<String> {
        self.prop_string("ReferenceData")
    }

    // ---- 瀛愮被瑙嗗浘锛堟寜绫诲瀷鍙栵級 ----

    pub fn user(&self) -> Option<IvgUserSnapPoint> {
        self.prop_dispatch("User").map(IvgUserSnapPoint::new)
    }

    pub fn object(&self) -> Option<IvgObjectSnapPoint> {
        self.prop_dispatch("Object").map(IvgObjectSnapPoint::new)
    }

    pub fn bbox(&self) -> Option<IvgBBoxSnapPoint> {
        self.prop_dispatch("BBox").map(IvgBBoxSnapPoint::new)
    }

    pub fn edge(&self) -> Option<IvgEdgeSnapPoint> {
        self.prop_dispatch("Edge").map(IvgEdgeSnapPoint::new)
    }

    // ---- 鎿嶄綔 ----

    pub fn delete(&self) -> bool {
        self.disp.invoke_method("Delete", vec![]).is_ok()
    }
}

// =============================================================
// IvgUserSnapPoint
// =============================================================

pub struct IvgUserSnapPoint {
    disp: ComObject,
}

impl IvgUserSnapPoint {
    pub fn new(disp: IDispatch) -> Self {
        Self { disp: ComObject::from_dispatch(disp, crate::types::IID_IDISPATCH) }
    }

    fn prop_string(&self, name: &str) -> Option<String> {
        self.disp.get_property(name).ok()?.to_string().ok()
    }

    fn prop_bool(&self, name: &str) -> Option<bool> {
        self.disp.get_property(name).ok()?.to_bool().ok()
    }

    fn put_bool(&self, name: &str, v: bool) -> bool {
        let arg = Variant::from_bool(v);
        self.disp.set_property(name, vec![arg]).is_ok()
    }

    pub fn id(&self) -> Option<String> { self.prop_string("ID") }

    pub fn auto_snap(&self) -> Option<bool> { self.prop_bool("AutoSnap") }
    pub fn set_auto_snap(&self, v: bool) -> bool { self.put_bool("AutoSnap", v) }
}

// =============================================================
// IvgObjectSnapPoint
// =============================================================

pub struct IvgObjectSnapPoint {
    disp: ComObject,
}

impl IvgObjectSnapPoint {
    pub fn new(disp: IDispatch) -> Self {
        Self { disp: ComObject::from_dispatch(disp, crate::types::IID_IDISPATCH) }
    }

    /// `cdrObjectSnapPointType`
    pub fn snap_type(&self) -> Option<i64> {
        self.disp.get_property("Type").ok()?.to_i64().ok()
    }
}

// =============================================================
// IvgBBoxSnapPoint
// =============================================================

pub struct IvgBBoxSnapPoint {
    disp: ComObject,
}

impl IvgBBoxSnapPoint {
    pub fn new(disp: IDispatch) -> Self {
        Self { disp: ComObject::from_dispatch(disp, crate::types::IID_IDISPATCH) }
    }

    /// `cdrReferencePoint`
    pub fn snap_type(&self) -> Option<i64> {
        self.disp.get_property("Type").ok()?.to_i64().ok()
    }
}

// =============================================================
// IvgEdgeSnapPoint
// =============================================================

pub struct IvgEdgeSnapPoint {
    disp: ComObject,
}

impl IvgEdgeSnapPoint {
    pub fn new(disp: IDispatch) -> Self {
        Self { disp: ComObject::from_dispatch(disp, crate::types::IID_IDISPATCH) }
    }

    fn prop_i64(&self, name: &str) -> Option<i64> {
        self.disp.get_property(name).ok()?.to_i64().ok()
    }

    fn prop_f64(&self, name: &str) -> Option<f64> {
        self.disp.get_property(name).ok()?.to_f64().ok()
    }

    pub fn segment_index(&self) -> Option<i64> { self.prop_i64("SegmentIndex") }
    pub fn segment_offset(&self) -> Option<f64> { self.prop_f64("SegmentOffset") }
}

// =============================================================
// IvgSnapPoints
// =============================================================

pub struct IvgSnapPoints {
    disp: ComObject,
}

impl IvgSnapPoints {
    pub fn new(disp: IDispatch) -> Self {
        Self { disp: ComObject::from_dispatch(disp, crate::types::IID_IDISPATCH) }
    }

    fn prop_i64(&self, name: &str) -> Option<i64> {
        self.disp.get_property(name).ok()?.to_i64().ok()
    }

    fn prop_dispatch(&self, name: &str) -> Option<IDispatch> {
        self.disp.get_property(name).ok()?.to_idispatch().ok()
    }

    pub fn count(&self) -> Option<i64> { self.prop_i64("Count") }

    pub fn item(&self, index: i32) -> Option<IvgSnapPoint> {
        let args = vec![Variant::from_i64(index as i64)];
        self.disp
            .invoke_method("Item", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgSnapPoint::new)
    }

    pub fn all(&self) -> Vec<IvgSnapPoint> {
        let n = self.count().unwrap_or(0) as i32;
        (1..=n).filter_map(|i| self.item(i)).collect()
    }

    // ---- 鍒嗙被鑾峰彇 ----

    pub fn user(&self, id: impl Into<String>) -> Option<IvgSnapPoint> {
        let args = vec![Variant::from_str(id.into())];
        self.disp
            .invoke_method("User", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgSnapPoint::new)
    }

    /// `cdrReferencePoint`
    pub fn bbox(&self, ref_point: i32) -> Option<IvgSnapPoint> {
        let args = vec![Variant::from_i64(ref_point as i64)];
        self.disp
            .invoke_method("BBox", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgSnapPoint::new)
    }

    /// `cdrObjectSnapPointType`
    pub fn object(&self, snap_type: i32) -> Option<IvgSnapPoint> {
        let args = vec![Variant::from_i64(snap_type as i64)];
        self.disp
            .invoke_method("Object", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgSnapPoint::new)
    }

    pub fn edge(&self, segment_index: i32, segment_offset: f64) -> Option<IvgSnapPoint> {
        let args = vec![
            Variant::from_i64(segment_index as i64),
            Variant::from_f64(segment_offset),
        ];
        self.disp
            .invoke_method("Edge", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgSnapPoint::new)
    }

    pub fn auto_snap_point(&self) -> Option<IvgSnapPoint> {
        self.disp
            .invoke_method("Auto", vec![])
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgSnapPoint::new)
    }

    pub fn find_closest(
        &self,
        type_set: i32,
        x: f64,
        y: f64,
    ) -> Option<IvgSnapPoint> {
        let args = vec![
            Variant::from_i64(type_set as i64),
            Variant::from_f64(x),
            Variant::from_f64(y),
        ];
        self.disp
            .invoke_method("FindClosest", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgSnapPoint::new)
    }

    // ---- 鑼冨洿 ----

    /// 鎸夊紩鐢ㄦ暟缁勫彇鑼冨洿銆?
    pub fn range(&self, references: Variant) -> Option<IvgSnapPointRange> {
        let args = vec![references];
        self.disp
            .invoke_method("Range", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgSnapPointRange::new)
    }

    pub fn selection(&self) -> Option<IvgSnapPointRange> {
        self.prop_dispatch("Selection").map(IvgSnapPointRange::new)
    }

    pub fn all_range(&self) -> Option<IvgSnapPointRange> {
        self.prop_dispatch("All").map(IvgSnapPointRange::new)
    }

    pub fn clear_selection(&self) -> bool {
        self.disp.invoke_method("ClearSelection", vec![]).is_ok()
    }

    // ---- 娣诲姞鐢ㄦ埛鎶撳彇鐐?----

    pub fn add_user_snap_point(
        &self,
        x: f64,
        y: f64,
        direction: f64,
        use_direction: bool,
    ) -> Option<IvgSnapPoint> {
        let args = vec![
            Variant::from_f64(x),
            Variant::from_f64(y),
            Variant::from_f64(direction),
            Variant::from_bool(use_direction),
        ];
        self.disp
            .invoke_method("AddUserSnapPoint", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgSnapPoint::new)
    }

    pub fn add_user_snap_point_ex(
        &self,
        id: impl Into<String>,
        x: f64,
        y: f64,
        direction: f64,
        use_direction: bool,
    ) -> Option<IvgSnapPoint> {
        let args = vec![
            Variant::from_str(id.into()),
            Variant::from_f64(x),
            Variant::from_f64(y),
            Variant::from_f64(direction),
            Variant::from_bool(use_direction),
        ];
        self.disp
            .invoke_method("AddUserSnapPointEx", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgSnapPoint::new)
    }
}

// =============================================================
// IvgSnapPointRange
// =============================================================

pub struct IvgSnapPointRange {
    disp: ComObject,
}

impl IvgSnapPointRange {
    pub fn new(disp: IDispatch) -> Self {
        Self { disp: ComObject::from_dispatch(disp, crate::types::IID_IDISPATCH) }
    }

    fn prop_i64(&self, name: &str) -> Option<i64> {
        self.disp.get_property(name).ok()?.to_i64().ok()
    }

    pub fn count(&self) -> Option<i64> { self.prop_i64("Count") }

    pub fn item(&self, index: i32) -> Option<IvgSnapPoint> {
        let args = vec![Variant::from_i64(index as i64)];
        self.disp
            .invoke_method("Item", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgSnapPoint::new)
    }

    pub fn all(&self) -> Vec<IvgSnapPoint> {
        let n = self.count().unwrap_or(0) as i32;
        (1..=n).filter_map(|i| self.item(i)).collect()
    }

    pub fn move_by(&self, dx: f64, dy: f64) -> bool {
        let args = vec![
            Variant::from_f64(dx),
            Variant::from_f64(dy),
        ];
        self.disp.invoke_method("Move", args).is_ok()
    }

    pub fn delete(&self) -> bool {
        self.disp.invoke_method("Delete", vec![]).is_ok()
    }

    pub fn add(&self, sp: &IvgSnapPoint) -> Option<bool> {
        let args = vec![sp.as_variant()];
        self.disp.invoke_method("Add", args).ok()?.to_bool().ok()
    }

    pub fn remove(&self, index: i32) -> Option<bool> {
        let args = vec![Variant::from_i64(index as i64)];
        self.disp.invoke_method("Remove", args).ok()?.to_bool().ok()
    }

    pub fn remove_by_reference(&self, reference_data: impl Into<String>) -> Option<bool> {
        let args = vec![Variant::from_str(reference_data.into())];
        self.disp
            .invoke_method("RemoveByReference", args)
            .ok()?
            .to_bool()
            .ok()
    }

    pub fn find(&self, reference_data: impl Into<String>) -> Option<IvgSnapPoint> {
        let args = vec![Variant::from_str(reference_data.into())];
        self.disp
            .invoke_method("Find", args)
            .ok()?
            .to_idispatch()
            .ok()
            .map(IvgSnapPoint::new)
    }

    pub fn create_selection(&self) -> bool {
        self.disp.invoke_method("CreateSelection", vec![]).is_ok()
    }

    pub fn add_to_selection(&self) -> bool {
        self.disp.invoke_method("AddToSelection", vec![]).is_ok()
    }

    pub fn remove_from_selection(&self) -> bool {
        self.disp.invoke_method("RemoveFromSelection", vec![]).is_ok()
    }

    pub fn change_direction(&self, direction: f64, uses_direction: i32) -> bool {
        let args = vec![
            Variant::from_f64(direction),
            Variant::from_i64(uses_direction as i64),
        ];
        self.disp.invoke_method("ChangeDirection", args).is_ok()
    }

    pub fn set_auto_snap(&self, v: bool) -> bool {
        let args = vec![Variant::from_bool(v)];
        self.disp.invoke_method("SetAutoSnap", args).is_ok()
    }
}