File size: 10,344 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
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
/***************************************************************************

 *   Copyright (c) 2011 Juergen Riegel <FreeCAD@juergen-riegel.net>        *

 *                                                                         *

 *   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                                *

 *                                                                         *

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


#ifndef PARTDESIGN_Hole_H
#define PARTDESIGN_Hole_H

#include <optional>
#include <App/PropertyUnits.h>
#include "json_fwd.hpp"
#include "FeatureSketchBased.h"

class Property;

namespace Base
{
class XMLReader;
}

namespace PartDesign
{

static constexpr size_t ThreadClass_ISOmetric_data_size = 25;
static constexpr size_t ThreadRunout_size = 24;

class PartDesignExport Hole: public ProfileBased
{
    PROPERTY_HEADER_WITH_OVERRIDE(PartDesign::Hole);

public:
    Hole();

    App::PropertyBool Threaded;
    App::PropertyBool ModelThread;
    App::PropertyLength ThreadPitch;
    App::PropertyEnumeration ThreadType;
    App::PropertyEnumeration ThreadSize;
    App::PropertyEnumeration ThreadClass;
    App::PropertyEnumeration ThreadFit;
    App::PropertyLength Diameter;
    App::PropertyLength ThreadDiameter;
    App::PropertyEnumeration ThreadDirection;
    App::PropertyEnumeration HoleCutType;
    App::PropertyBool HoleCutCustomValues;
    App::PropertyLength HoleCutDiameter;
    App::PropertyLength HoleCutDepth;
    App::PropertyAngle HoleCutCountersinkAngle;
    App::PropertyEnumeration DepthType;
    App::PropertyLength Depth;
    App::PropertyEnumeration ThreadDepthType;
    App::PropertyLength ThreadDepth;
    App::PropertyEnumeration DrillPoint;
    App::PropertyAngle DrillPointAngle;
    App::PropertyBool DrillForDepth;
    App::PropertyBool Tapered;
    App::PropertyAngle TaperedAngle;
    App::PropertyBool UseCustomThreadClearance;
    App::PropertyLength CustomThreadClearance;
    App::PropertyInteger BaseProfileType;

    enum BaseProfileTypeOptions

    {
        OnPoints = 1 << 0,
        OnCircles = 1 << 1,
        OnArcs = 1 << 2,

        // Common combos
        OnPointsCirclesArcs = OnPoints | OnCircles | OnArcs,
        OnCirclesArcs = OnCircles | OnArcs
    };
    static int baseProfileOption_idxToBitmask(int index);
    static int baseProfileOption_bitmaskToIdx(int bitmask);

    /** @name methods override feature */
    //@{
    /// recalculate the feature
    App::DocumentObjectExecReturn* execute() override;

    /// returns the type name of the view provider
    const char* getViewProviderName() const override
    {
        return "PartDesignGui::ViewProviderHole";
    }
    //@}
    short mustExecute() const override;

    using ThreadDescription = struct
    {
        const char* designation;
        double diameter;
        double pitch;
        double TapDrill;
    };
    static const std::vector<Hole::ThreadDescription> threadDescription[];

    static const double metricHoleDiameters[51][4];

    using UTSClearanceDefinition = struct
    {
        std::string designation;
        double close;
        double normal;
        double loose;
    };
    static const UTSClearanceDefinition UTSHoleDiameters[23];

    void Restore(Base::XMLReader& reader) override;

    virtual void updateProps();
    bool isDynamicCounterbore(const std::string& thread, const std::string& holeCutType);
    bool isDynamicCountersink(const std::string& thread, const std::string& holeCutType);

    Base::Vector3d guessNormalDirection(const TopoShape& profileshape) const;
    TopoShape findHoles(

        std::vector<TopoShape>& holes,

        const TopoShape& profileshape,

        const TopoDS_Shape& protohole

    ) const;

protected:
    void onChanged(const App::Property* prop) override;
    void setupObject() override;

    static const App::PropertyAngle::Constraints floatAngle;

private:
    static const char* DepthTypeEnums[];
    static const char* ThreadDepthTypeEnums[];
    static const char* ThreadTypeEnums[];
    static const char* ClearanceNoneEnums[];
    static const char* ClearanceMetricEnums[];
    static const char* ClearanceUTSEnums[];
    static const char* ClearanceOtherEnums[];
    static const char* DrillPointEnums[];
    static const char* ThreadDirectionEnums[];

    /* "None" thread profile */
    static const char* HoleCutType_None_Enums[];
    static const char* ThreadClass_None_Enums[];

    /* ISO metric coarse profile */
    static std::vector<std::string> HoleCutType_ISOmetric_Enums;
    static const char* ThreadClass_ISOmetric_Enums[];
    static const double ThreadClass_ISOmetric_data[ThreadClass_ISOmetric_data_size][2];

    /* ISO metric fine profile */
    static std::vector<std::string> HoleCutType_ISOmetricfine_Enums;
    static const char* ThreadClass_ISOmetricfine_Enums[];

    /* UNC profile */
    static const char* HoleCutType_UNC_Enums[];
    static const char* ThreadClass_UNC_Enums[];

    /* UNF profile */
    static const char* HoleCutType_UNF_Enums[];
    static const char* ThreadClass_UNF_Enums[];

    /* UNEF profile */
    static const char* HoleCutType_UNEF_Enums[];
    static const char* ThreadClass_UNEF_Enums[];

    /* NPT profile */
    static const char* HoleCutType_NPT_Enums[];

    /* BSP profile */
    static const char* HoleCutType_BSP_Enums[];

    /* BSW profile */
    static const char* HoleCutType_BSW_Enums[];
    static const char* ThreadClass_BSW_Enums[];

    /* BSF profile */
    static const char* HoleCutType_BSF_Enums[];
    static const char* ThreadClass_BSF_Enums[];

    static const double ThreadRunout[ThreadRunout_size][2];

    /* Counter-xxx */
    // public:
    //  Dimension for counterbore
    struct CounterBoreDimension

    {
        std::string thread;
        double diameter;
        double depth;
        static const CounterBoreDimension nothing;
    };
    // Dimension for countersink
    struct CounterSinkDimension

    {
        std::string thread;
        double diameter;
        static const CounterSinkDimension nothing;
    };

    // cut dimensions for a screwtype
    class CutDimensionSet

    {
    public:
        enum CutType

        {
            Counterbore,
            Countersink
        };
        enum ThreadType

        {
            Metric,
            MetricFine
        };

        CutDimensionSet()
            : cut_type(Counterbore)
            , thread_type(Metric)
            , angle(0.0)
        {}
        CutDimensionSet(
            const std::string& nme,
            std::vector<CounterBoreDimension>&& d,
            CutType cut,
            ThreadType thread,
            double angle = 0.0
        );
        CutDimensionSet(
            const std::string& nme,
            std::vector<CounterSinkDimension>&& d,
            CutType cut,
            ThreadType thread,
            double angle = 0.0
        );

        const CounterBoreDimension& get_bore(const std::string& t) const;
        const CounterSinkDimension& get_sink(const std::string& t) const;

        std::vector<CounterBoreDimension> bore_data;
        std::vector<CounterSinkDimension> sink_data;
        CutType cut_type;
        ThreadType thread_type;
        std::string name;
        double angle;
    };

    class CutDimensionKey

    {
        std::string thread_type;
        std::string cut_name;

    public:
        CutDimensionKey() = default;
        CutDimensionKey(const std::string& t, const std::string& c);
        bool operator<(const CutDimensionKey& b) const;
    };

    std::map<CutDimensionKey, CutDimensionSet> HoleCutTypeMap;

    const CutDimensionSet& find_cutDimensionSet(const std::string& t, const std::string& c);

    const CutDimensionSet& find_cutDimensionSet(const CutDimensionKey& k);

    void addCutType(const CutDimensionSet& dimensions);
    void updateHoleCutParams();
    void calculateAndSetCounterbore();
    void calculateAndSetCountersink();
    std::optional<double> determineDiameter() const;
    void updateDiameterParam();
    void updateThreadDepthParam();
    void readCutDefinitions();

    double getCountersinkAngle() const;
    double getThreadClassClearance() const;
    double getThreadRunout(int mode = 1) const;
    double getThreadPitch() const;
    double getThreadProfileAngle();
    void findClosestDesignation();
    void rotateToNormal(const gp_Dir& helixAxis, const gp_Dir& normalAxis, TopoDS_Shape& helixShape) const;
    gp_Vec computePerpendicular(const gp_Vec&) const;
    TopoDS_Shape makeThread(const gp_Vec&, const gp_Vec&, double);

    // helpers for nlohmann json
    friend void from_json(const nlohmann::json& j, CounterBoreDimension& t);
    friend void from_json(const nlohmann::json& j, CounterSinkDimension& t);
    friend void from_json(const nlohmann::json& j, CutDimensionSet& t);
};

}  // namespace PartDesign


#endif  // PART_Hole_H