From ecfece6d2a9d8761737228afe1ef2393c5f28ecd Mon Sep 17 00:00:00 2001 From: fraguada Date: Tue, 19 Dec 2023 16:14:26 +0100 Subject: [PATCH] first pass at bindings AddInstanceDefinition --- src/bindings/bnd_extensions.cpp | 85 +++++++++++++++++++++++++++++++-- src/bindings/bnd_extensions.h | 1 + 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/src/bindings/bnd_extensions.cpp b/src/bindings/bnd_extensions.cpp index 0362082a..11f5baf8 100644 --- a/src/bindings/bnd_extensions.cpp +++ b/src/bindings/bnd_extensions.cpp @@ -1144,12 +1144,90 @@ void BND_File3dmInstanceDefinitionTable::Add(const BND_InstanceDefinitionGeometr m_model->AddModelComponent(*_idef); } -/* -void BND_File3dmInstanceDefinitionTable::Add(std::wstring name, std::wstring description, std::wstring url, std::wstring urlTag, ON_3dPoint basePoint, const std::vector& geometry, const std::vector& attributes) + +int BND_File3dmInstanceDefinitionTable::AddInstanceDefinition(std::wstring name, std::wstring description, std::wstring url, std::wstring url_tag, ON_3dPoint basePoint, const std::vector& geometry, const std::vector& attributes) { + const int count_g = (int)geometry.size(); + const int count_a = (int)attributes.size(); + int index = -1; + + if(m_model) + { + + // Determine if we need to transform geometry to world origin + ON_Xform xf; + ON_Xform* pXform = nullptr; + if (basePoint.IsValid() && basePoint != ON_3dPoint::Origin) + { + xf = ON_Xform::TranslationTransformation(ON_3dPoint::Origin - basePoint); + pXform = &xf; + } + + ON_SimpleArray object_uuids; + + for ( int i = 0; i < count_g; i ++ ) + { + const ON_Geometry* pConstGeom = &geometry[i]; + const ON_3dmObjectAttributes* pConstAtts = i < count_a ? &attributes[i] : &ON_3dmObjectAttributes::DefaultAttributes; + + if (pConstGeom && pConstAtts) + { + ON_Geometry* pGeom = pConstGeom->Duplicate(); // Copy so we can transform + if (pGeom) + { + // Make certain that proper flags are set for instance definiton geometry + ON_3dmObjectAttributes atts(*pConstAtts); + atts.m_uuid = ON_nil_uuid; + atts.SetMode(ON::object_mode::idef_object); + atts.RemoveFromAllGroups(); + atts.m_space = ON::model_space; + atts.m_viewport_id = ON_nil_uuid; + + // Transform if needed + if (pXform) + { + atts.Transform(pGeom, *pXform); + pGeom->Transform(*pXform); + } + + BND_3dmObjectAttributes _atts; + _atts.m_attributes = &atts; + ON_UUID uuid = Internal_ONX_Model_AddModelGeometry(m_model.get(), pGeom, &_atts); + if (ON_UuidIsNotNil(uuid)) + object_uuids.Append(uuid); + + delete pGeom; // Don't leak... + } + } + + } + + if (object_uuids.Count()) + { + ON_InstanceDefinition* idef = new ON_InstanceDefinition(); + if (nullptr != idef) + { + idef->SetInstanceGeometryIdList(object_uuids); + idef->SetInstanceDefinitionType(ON_InstanceDefinition::IDEF_UPDATE_TYPE::Static); + idef->SetName(name.c_str()); + idef->SetDescription(description.c_str()); + idef->SetURL(url.c_str()); + idef->SetURL_Tag(url_tag.c_str()); + ON_ModelComponentReference model_component_reference = m_model->AddManagedModelComponent(idef, true); + if (!model_component_reference.IsEmpty()) + { + const ON_ModelComponent* model_component = model_component_reference.ModelComponent(); + if (nullptr != model_component) + index = model_component->Index(); + } + } + } + } + + return index; } -*/ + BND_InstanceDefinitionGeometry* BND_File3dmInstanceDefinitionTable::FindIndex(int index) const { @@ -1612,6 +1690,7 @@ void initExtensionsBindings(pybind11::module& m) .def("__getitem__", &BND_File3dmInstanceDefinitionTable::FindIndex) .def("__iter__", [](py::object s) { return PyBNDIterator(s.cast(), s); }) .def("Add", &BND_File3dmInstanceDefinitionTable::Add, py::arg("idef")) + .def("AddInstanceDefinition", &BND_File3dmInstanceDefinitionTable::AddInstanceDefinition, py::arg("name"), py::arg("description"), py::arg("url"), py::arg("urlTag"), py::arg("basePoint"), py::arg("geometry"), py::arg("attributes")) .def("FindIndex", &BND_File3dmInstanceDefinitionTable::FindIndex, py::arg("index")) .def("FindId", &BND_File3dmInstanceDefinitionTable::FindId, py::arg("id")) ; diff --git a/src/bindings/bnd_extensions.h b/src/bindings/bnd_extensions.h index 74304c87..97ad83a8 100644 --- a/src/bindings/bnd_extensions.h +++ b/src/bindings/bnd_extensions.h @@ -188,6 +188,7 @@ class BND_File3dmInstanceDefinitionTable BND_File3dmInstanceDefinitionTable(std::shared_ptr m) { m_model = m; } int Count() const { return m_model.get()->ActiveComponentCount(ON_ModelComponent::Type::InstanceDefinition); } void Add(const class BND_InstanceDefinitionGeometry& idef); + int AddInstanceDefinition(std::wstring name, std::wstring description, std::wstring url, std::wstring url_tag, ON_3dPoint basePoint, const std::vector& geometry, const std::vector& attributes); // public int Add(string name, string description, string url, string urlTag, Point3d basePoint, IEnumerable geometry, IEnumerable attributes) //int Add(std::wstring name, std::wstring description, std::wstring url, std::wstring urlTag, ON_3dPoint basePoint, const std::vector& geometry, const std::vector& attributes); // public int AddLinked(string filename, string name, string description)