File size: 6,838 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************

 *   Copyright (c) 2020 Abdullah Tahiri <abdullah.tahiri.yo@gmail.com>     *

 *                                                                         *

 *   This file is part of the FreeCAD CAx development system.              *

 *                                                                         *

 *   This library is free software; you can redistribute it and/or         *

 *   modify it under the terms of the GNU Library General Public           *

 *   License as published by the Free Software Foundation; either          *

 *   version 2 of the License, or (at your option) any later version.      *

 *                                                                         *

 *   This library  is distributed in the hope that it will be useful,      *

 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *

 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *

 *   GNU Library General Public License for more details.                  *

 *                                                                         *

 *   You should have received a copy of the GNU Library General Public     *

 *   License along with this library; see the file COPYING.LIB. If not,    *

 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *

 *   Suite 330, Boston, MA  02111-1307, USA                                *

 *                                                                         *

 ***************************************************************************/

#include <boost/uuid/uuid_io.hpp>


#include "GeometryFacade.h"
#include "GeometryFacadePy.h"


using namespace Sketcher;

TYPESYSTEM_SOURCE(Sketcher::GeometryFacade, Base::BaseClass)

GeometryFacade::GeometryFacade()
    : Geo(nullptr)
    , OwnerGeo(false)
    , SketchGeoExtension(nullptr)
{}

GeometryFacade::GeometryFacade(const Part::Geometry* geometry, bool owner)
    : Geo(geometry)
    , OwnerGeo(owner)
{
    assert(geometry);  // This should never be nullptr, as this constructor is protected

    initExtension();
}

GeometryFacade::~GeometryFacade()
{
    if (OwnerGeo && Geo) {
        delete Geo;
    }
}

std::unique_ptr<GeometryFacade> GeometryFacade::getFacade(const Part::Geometry* geometry, bool owner)

{
    if (geometry) {
        return std::unique_ptr<GeometryFacade>(new GeometryFacade(geometry, owner));
    }
    else {
        return std::unique_ptr<GeometryFacade>(nullptr);
    }
    // make_unique has no access to private constructor
    // return std::make_unique<GeometryFacade>(geometry);
}

void GeometryFacade::setGeometry(Part::Geometry* geometry)

{
    Geo = geometry;

    if (geometry) {
        initExtension();
    }
    else {
        THROWM(Base::ValueError, "GeometryFacade initialized with Geometry null pointer");
    }
}

void GeometryFacade::initExtension()

{
    if (!Geo->hasExtension(SketchGeometryExtension::getClassTypeId())) {

        getGeo()->setExtension(std::make_unique<SketchGeometryExtension>());  // Create getExtension

        // Base::Console().warning("%s\nSketcher Geometry without Extension: %s \n",
        // boost::uuids::to_string(Geo->getTag()).c_str());
    }

    SketchGeoExtension = std::static_pointer_cast<const SketchGeometryExtension>(
        (Geo->getExtension(SketchGeometryExtension::getClassTypeId())).lock()
    );
}

void GeometryFacade::initExtension() const

{
    // const Geometry without SketchGeometryExtension cannot initialise a GeometryFacade
    if (!Geo->hasExtension(SketchGeometryExtension::getClassTypeId())) {
        THROWM(
            Base::ValueError,
            "Cannot create a GeometryFacade out of a const Geometry pointer not having a "
            "SketchGeometryExtension!"
        );
    }

    auto ext = std::static_pointer_cast<const SketchGeometryExtension>(
        Geo->getExtension(SketchGeometryExtension::getClassTypeId()).lock()
    );

    const_cast<GeometryFacade*>(this)->SketchGeoExtension = ext;
}

void GeometryFacade::throwOnNullPtr(const Part::Geometry* geo)

{
    if (!geo) {
        THROWM(Base::ValueError, "Geometry is nullptr!");
    }
}

void GeometryFacade::ensureSketchGeometryExtension(Part::Geometry* geometry)

{
    throwOnNullPtr(geometry);

    if (!geometry->hasExtension(SketchGeometryExtension::getClassTypeId())) {
        geometry->setExtension(std::make_unique<SketchGeometryExtension>());  // Create getExtension
    }
}

void GeometryFacade::copyId(const Part::Geometry* src, Part::Geometry* dst)

{
    throwOnNullPtr(src);
    throwOnNullPtr(dst);

    auto gfsrc = GeometryFacade::getFacade(src);
    auto gfdst = GeometryFacade::getFacade(dst);
    gfdst->setId(gfsrc->getId());
}

int GeometryFacade::getId(const Part::Geometry* geometry)

{
    auto gf = GeometryFacade::getFacade(geometry);
    return gf->getId();
}

void GeometryFacade::setId(const Part::Geometry* geometry, int id)

{
    auto gf = GeometryFacade::getFacade(geometry);
    gf->setId(id);
}

bool GeometryFacade::getConstruction(const Part::Geometry* geometry)

{
    throwOnNullPtr(geometry);

    auto gf = GeometryFacade::getFacade(geometry);
    return gf->getConstruction();
}

void GeometryFacade::setConstruction(Part::Geometry* geometry, bool construction)

{
    throwOnNullPtr(geometry);

    auto gf = GeometryFacade::getFacade(geometry);
    gf->setConstruction(construction);
}

bool GeometryFacade::isInternalType(const Part::Geometry* geometry, InternalType::InternalType type)

{
    throwOnNullPtr(geometry);

    auto gf = GeometryFacade::getFacade(geometry);
    return gf->getInternalType() == type;
}

bool GeometryFacade::isInternalAligned(const Part::Geometry* geometry)

{
    throwOnNullPtr(geometry);

    auto gf = GeometryFacade::getFacade(geometry);
    return gf->isInternalAligned();
}

InternalType::InternalType GeometryFacade::getInternalType(const Part::Geometry* geometry)

{
    throwOnNullPtr(geometry);

    auto gf = GeometryFacade::getFacade(geometry);
    return gf->getInternalType();
}

void GeometryFacade::setInternalType(Part::Geometry* geometry, InternalType::InternalType type)

{
    throwOnNullPtr(geometry);

    auto gf = GeometryFacade::getFacade(geometry);
    gf->setInternalType(type);
}

bool GeometryFacade::getBlocked(const Part::Geometry* geometry)

{
    throwOnNullPtr(geometry);

    auto gf = GeometryFacade::getFacade(geometry);
    return gf->getBlocked();
}

PyObject* GeometryFacade::getPyObject()

{
    return new GeometryFacadePy(new GeometryFacade(this->Geo));
}