forked from 1b-agent/opencadstudio-upstream
IMPORT: 3D Studio files
A .3ds file opens the 3D Studio Import window: the file's objects with their kind and material count, filtered by kind or name, checked one by one or all at once, where to put them on layers and what a mesh with several materials becomes. Checked meshes come in as polyface meshes on the chosen layers with materials made from the file's colours, cameras as named views; a mesh with several materials is split, given its first material, given none, or asked about on the command line. Automation drives the window with tds_import_dialog. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9ddc103bcc
commit
78e429732e
35 files changed
+1893
-4
No files matched your search
@@ -203,3 +203,217 @@ impl OpenCADStudio {
|
||||
Task::none()
|
||||
}
|
||||
}
|
||||
|
||||
impl OpenCADStudio {
|
||||
/// The 3D Studio Import window for a file; an error when it is no 3D
|
||||
/// Studio file.
|
||||
pub(in crate::app) fn open_tds_import_dialog(&mut self, path: &str) -> Result<(), String> {
|
||||
let scene = crate::scene::model::pdf_raster::source_bytes(path)
|
||||
.and_then(|bytes| crate::io::tds_import::read(&bytes))
|
||||
.ok_or_else(|| format!("{} is not a valid 3D Studio file.", path.replace('/', "\\")))?;
|
||||
let mut objects: Vec<(String, &'static str, usize)> = scene
|
||||
.objects
|
||||
.iter()
|
||||
.map(|(name, object)| {
|
||||
let materials = match object {
|
||||
crate::io::tds_import::TdsObject::Mesh(mesh) => scene.mesh_materials(mesh).len(),
|
||||
_ => 0,
|
||||
};
|
||||
(name.clone(), object.kind(), materials)
|
||||
})
|
||||
.collect();
|
||||
objects.sort_by_key(|o| o.0.to_lowercase());
|
||||
objects.insert(0, ("Global Settings".to_string(), "Global", 0));
|
||||
// The layer and material choices made last stay; nothing starts
|
||||
// checked.
|
||||
let previous = self.import_dialogs.tds.take();
|
||||
self.import_dialogs.tds = Some(crate::ui::window::import_dialogs::TdsImportState {
|
||||
path: path.to_string(),
|
||||
checked: vec![false; objects.len()],
|
||||
objects,
|
||||
materials: scene.materials.len(),
|
||||
filter: 0,
|
||||
search: String::new(),
|
||||
layers: previous.as_ref().map_or(0, |s| s.layers),
|
||||
multi: previous.as_ref().map_or(0, |s| s.multi),
|
||||
});
|
||||
self.active_modal = Some(crate::app::ModalKind::TdsImport);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The window's choices as an import: asked mesh by mesh first when set
|
||||
/// to ask and some checked mesh has several materials.
|
||||
pub(in crate::app) fn start_tds_import(
|
||||
&mut self,
|
||||
i: usize,
|
||||
state: crate::ui::window::import_dialogs::TdsImportState,
|
||||
) -> Task<Message> {
|
||||
use crate::io::tds_import::{LayerMode, MultiMaterial, TdsObject};
|
||||
use crate::modules::insert::tds_import::{TdsImportRequest, TdsMaterialCommand};
|
||||
let Some(scene) = crate::scene::model::pdf_raster::source_bytes(&state.path)
|
||||
.and_then(|bytes| crate::io::tds_import::read(&bytes))
|
||||
else {
|
||||
return Task::none();
|
||||
};
|
||||
let selected: Vec<String> = state
|
||||
.objects
|
||||
.iter()
|
||||
.zip(&state.checked)
|
||||
.filter(|(_, on)| **on)
|
||||
.map(|((name, _, _), _)| name.clone())
|
||||
.collect();
|
||||
let several: Vec<(String, Vec<String>)> = scene
|
||||
.objects
|
||||
.iter()
|
||||
.filter(|(name, _)| selected.contains(name))
|
||||
.filter_map(|(name, object)| match object {
|
||||
TdsObject::Mesh(mesh) => {
|
||||
let materials = scene.mesh_materials(mesh);
|
||||
(materials.len() > 1).then(|| (name.clone(), materials))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
let choice = match state.multi {
|
||||
1 => Some(MultiMaterial::Split),
|
||||
2 => Some(MultiMaterial::First),
|
||||
3 => Some(MultiMaterial::Material(None)),
|
||||
_ => None,
|
||||
};
|
||||
let mut request = TdsImportRequest {
|
||||
path: state.path.clone(),
|
||||
selected,
|
||||
layers: match state.layers {
|
||||
1 => LayerMode::ByMaterial,
|
||||
2 => LayerMode::ByObjectColor,
|
||||
3 => LayerMode::Single,
|
||||
_ => LayerMode::ByObject,
|
||||
},
|
||||
multi: Default::default(),
|
||||
compact: matches!(state.multi, 0 | 1),
|
||||
};
|
||||
match choice {
|
||||
Some(choice) => {
|
||||
for (name, _) in several {
|
||||
request.multi.insert(name, choice.clone());
|
||||
}
|
||||
}
|
||||
None if !several.is_empty() => {
|
||||
use crate::command::CadCommand;
|
||||
let command = TdsMaterialCommand::new(request, several);
|
||||
self.command_line.push_info(&command.prompt());
|
||||
self.tabs[i].active_cmd = Some(Box::new(command));
|
||||
return Task::none();
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
self.run_tds_import(i, request);
|
||||
Task::none()
|
||||
}
|
||||
|
||||
/// The import itself, one undo step: the meshes on their layers with
|
||||
/// their materials, the cameras as named views.
|
||||
pub(in crate::app) fn run_tds_import(
|
||||
&mut self,
|
||||
i: usize,
|
||||
request: crate::modules::insert::tds_import::TdsImportRequest,
|
||||
) {
|
||||
use codec::objects::ObjectType;
|
||||
let Some(scene) = crate::scene::model::pdf_raster::source_bytes(&request.path)
|
||||
.and_then(|bytes| crate::io::tds_import::read(&bytes))
|
||||
else {
|
||||
self.command_line.push_error(&format!(
|
||||
"{} is not a valid 3D Studio file.",
|
||||
request.path.replace('/', "\\")
|
||||
));
|
||||
return;
|
||||
};
|
||||
let import = crate::io::tds_import::convert(
|
||||
&scene,
|
||||
&crate::io::tds_import::TdsOptions {
|
||||
selected: request.selected.clone(),
|
||||
layers: request.layers,
|
||||
multi: request.multi.clone(),
|
||||
compact: request.compact,
|
||||
},
|
||||
);
|
||||
self.push_undo_snapshot(i, "IMPORT");
|
||||
let document = &mut self.tabs[i].scene.document;
|
||||
// Materials by name: the drawing's own when it has one, else one
|
||||
// made from the file's colours.
|
||||
let mut materials: std::collections::HashMap<String, codec::Handle> = Default::default();
|
||||
for name in &import.materials {
|
||||
let existing = document.objects.iter().find_map(|(h, o)| match o {
|
||||
ObjectType::Material(m) if m.name.eq_ignore_ascii_case(name) => Some(*h),
|
||||
_ => None,
|
||||
});
|
||||
let handle = match existing {
|
||||
Some(h) => h,
|
||||
None => {
|
||||
let source = scene.materials.iter().find(|m| m.name == *name);
|
||||
let mut material = codec::objects::Material::new();
|
||||
material.handle = document.allocate_handle();
|
||||
material.owner = document.header.acad_material_dict_handle;
|
||||
material.name = name.clone();
|
||||
let color = |rgb: Option<[u8; 3]>| match rgb {
|
||||
Some([r, g, b]) => codec::objects::MaterialColor {
|
||||
flag: 1,
|
||||
factor: 1.0,
|
||||
rgb: Some(
|
||||
((0xC2u32 << 24) | (u32::from(r) << 16) | (u32::from(g) << 8) | u32::from(b))
|
||||
as i32,
|
||||
),
|
||||
},
|
||||
None => codec::objects::MaterialColor::default(),
|
||||
};
|
||||
material.ambient_color = color(source.and_then(|m| m.ambient));
|
||||
material.diffuse_color = color(source.and_then(|m| m.diffuse));
|
||||
material.specular_color = color(source.and_then(|m| m.specular));
|
||||
let handle = material.handle;
|
||||
let dictionary = document.header.acad_material_dict_handle;
|
||||
if let Some(ObjectType::Dictionary(dictionary)) = document.objects.get_mut(&dictionary) {
|
||||
dictionary.add_entry(name.clone(), handle);
|
||||
}
|
||||
document.objects.insert(handle, ObjectType::Material(material));
|
||||
handle
|
||||
}
|
||||
};
|
||||
materials.insert(name.clone(), handle);
|
||||
}
|
||||
for part in &import.parts {
|
||||
if !document.layers.contains(&part.layer) {
|
||||
let mut layer = codec::tables::Layer::new(part.layer.as_str());
|
||||
layer.handle = document.allocate_handle();
|
||||
let _ = document.layers.add(layer);
|
||||
}
|
||||
}
|
||||
for view in &import.views {
|
||||
let mut named = codec::tables::View::new(view.name.as_str());
|
||||
named.target = codec::types::Vector3::new(view.target[0], view.target[1], view.target[2]);
|
||||
named.direction =
|
||||
codec::types::Vector3::new(view.direction[0], view.direction[1], view.direction[2]);
|
||||
named.height = view.height;
|
||||
named.width = view.height;
|
||||
named.lens_length = view.lens;
|
||||
named.handle = match document.views.get(&view.name) {
|
||||
Some(old) => old.handle,
|
||||
None => document.allocate_handle(),
|
||||
};
|
||||
document.views.add_or_replace(named);
|
||||
}
|
||||
for part in import.parts {
|
||||
let mut mesh = part.mesh;
|
||||
mesh.common.layer = part.layer.clone();
|
||||
mesh.common.material_handle = part.material.as_ref().and_then(|m| materials.get(m).copied());
|
||||
if mesh.common.material_handle.is_some() {
|
||||
mesh.common.material_flags = 0b11;
|
||||
}
|
||||
self.tabs[i].scene.add_entity(codec::EntityType::PolyfaceMesh(mesh));
|
||||
}
|
||||
for line in &import.messages {
|
||||
self.command_line.push_output(line);
|
||||
}
|
||||
self.tabs[i].dirty = true;
|
||||
self.refresh_properties();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user