catia 0.1.1

Rust COM bindings and helpers for CATIA V5
Documentation
use catia::{ComApartment, CatiaApp};
use catia::catia::{CatiaApi, CatiaMock};
use catia::com::is_progid_registered;
use windows::core::Result as WinResult;

fn main() -> WinResult<()> {
    let _apartment = ComApartment::init_sta()?;

    let args: Vec<String> = std::env::args().collect();
    let use_mock = args.iter().any(|a| a == "--mock");
    let progid = args
        .iter()
        .find_map(|a| a.strip_prefix("--progid="))
        .unwrap_or("CATIA.Application");
    let message = args
        .iter()
        .find_map(|a| a.strip_prefix("--msg="))
        .unwrap_or("请选择元素");
    let multi = args.iter().any(|a| a == "--multi");
    let types: Vec<&str> = args
        .iter()
        .filter_map(|a| a.strip_prefix("--type=") )
        .collect::<Vec<_>>();
    let types: Vec<&str> = if types.is_empty() { vec!["Face", "Edge", "Vertex"] } else { types };

    if !use_mock {
        match is_progid_registered(progid) {
            Ok(true) => {}
            Ok(false) => {
                eprintln!(
                    "未检测到 CATIA COM 环境(ProgID: {})。\n\
                    提示:请在已安装并注册 CATIA 的环境运行,或使用 --mock 参数。",
                    progid
                );
                return Ok(());
            }
            Err(err) => {
                eprintln!(
                    "检测 COM 环境时发生错误:{:?}。\n\
                    提示:可使用 --mock 参数运行以跳过 COM。",
                    err
                );
                return Ok(());
            }
        }
    }

    if use_mock {
        let api = CatiaMock::new();
        if let Some(count) = api.selection_select_element2(&types, message, multi)? {
            println!("选择完成,数量: {}", count);
            for i in 1..=count {
                if let Some((typ, name, leaf)) = api.selection_item_detail(i)? {
                    println!("- [{}] '{}' (叶产品: {:?})", typ, name, leaf);
                }
            }
        }
    } else {
        let app = if progid == "CATIA.Application" { CatiaApp::new()? } else { CatiaApp::with_progid(progid)? };
        let api: &dyn CatiaApi = &app;
        if let Some(count) = api.selection_select_element2(&types, message, multi)? {
            println!("选择完成,数量: {}", count);
            for i in 1..=count {
                if let Some((typ, name, leaf)) = api.selection_item_detail(i)? {
                    println!("- [{}] '{}' (叶产品: {:?})", typ, name, leaf);
                }
            }
        }
    }

    Ok(())
}