File size: 11,311 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
// SPDX-License-Identifier: LGPL-2.1-or-later

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

 *   Copyright (c) 2010 Jürgen Riegel <juergen.riegel@web.de>              *

 *                                                                         *

 *   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 <BRepAdaptor_Curve.hxx>
#include <CPnts_AbscissaPoint.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Edge.hxx>


#include <Base/Sequencer.h>
#include <Mod/Part/App/PartFeature.h>
#include <Mod/Part/App/edgecluster.h>

#include "Edge2TracObject.h"
#include "Trajectory.h"
#include "Waypoint.h"


using namespace Robot;
using namespace App;

PROPERTY_SOURCE(Robot::Edge2TracObject, Robot::TrajectoryObject)


Edge2TracObject::Edge2TracObject()
{

    ADD_PROPERTY_TYPE(Source, (nullptr), "Edge2Trac", Prop_None, "Edges to generate the Trajectory");
    ADD_PROPERTY_TYPE(SegValue, (0.5), "Edge2Trac", Prop_None, "Max deviation from original geometry");
    ADD_PROPERTY_TYPE(UseRotation, (0), "Edge2Trac", Prop_None, "use orientation of the edge");
    NbrOfEdges = 0;
    NbrOfCluster = 0;
}

App::DocumentObjectExecReturn* Edge2TracObject::execute()

{
    App::DocumentObject* link = Source.getValue();
    if (!link) {
        return new App::DocumentObjectExecReturn("No object linked");
    }
    if (!link->isDerivedFrom<Part::Feature>()) {
        return new App::DocumentObjectExecReturn("Linked object is not a Part object");
    }
    Part::Feature* base = static_cast<Part::Feature*>(Source.getValue());
    const Part::TopoShape& TopShape = base->Shape.getShape();

    const std::vector<std::string>& SubVals = Source.getSubValuesStartsWith("Edge");
    if (SubVals.empty()) {
        return new App::DocumentObjectExecReturn("No Edges specified");
    }

    // container for all the edges
    std::vector<TopoDS_Edge> edges;

    // run through the edge name and get the real objects from the TopoShape
    for (const auto& SubVal : SubVals) {
        TopoDS_Edge edge = TopoDS::Edge(TopShape.getSubShape(SubVal.c_str()));
        edges.push_back(edge);
    }

    // instantiate an edge cluster sorter and get the result
    Part::Edgecluster acluster(edges);
    Part::tEdgeClusterVector aclusteroutput = acluster.GetClusters();

    if (aclusteroutput.empty()) {
        return new App::DocumentObjectExecReturn("No Edges specified");
    }

    // set the number of cluster and edges
    NbrOfCluster = aclusteroutput.size();
    NbrOfEdges = 0;
    for (const auto& it : aclusteroutput) {
        NbrOfEdges += it.size();
    }

    // trajectory to fill
    Robot::Trajectory trac;
    bool first = true;

    // cycle through the cluster
    for (const auto& it : aclusteroutput) {
        // cycle through the edges of the cluster
        for (const auto& it2 : it) {
            BRepAdaptor_Curve adapt(it2);

            switch (adapt.GetType()) {
                case GeomAbs_Line: {
                    // get start and end point
                    gp_Pnt P1 = adapt.Value(adapt.FirstParameter());
                    gp_Pnt P2 = adapt.Value(adapt.LastParameter());

                    Base::Rotation R1;
                    Base::Rotation R2;

                    // if orientation is used
                    if (UseRotation.getValue()) {
                        // here get the orientation of the start and end point...
                        // R1 = ;
                        // R2 = ;
                    }

                    // if reverse orintation, switch the points
                    if (it2.Orientation() == TopAbs_REVERSED) {
                        // switch the points and orientation
                        gp_Pnt tmpP = P1;
                        Base::Rotation tmpR = R1;
                        P1 = P2;
                        R1 = R2;
                        R2 = tmpR;
                        P2 = tmpP;
                    }
                    if (first) {
                        Waypoint wp("Pt", Base::Placement(Base::Vector3d(P1.X(), P1.Y(), P1.Z()), R1));
                        trac.addWaypoint(wp);
                        first = false;
                    }
                    Waypoint wp("Pt", Base::Placement(Base::Vector3d(P2.X(), P2.Y(), P2.Z()), R2));
                    trac.addWaypoint(wp);
                    break;
                }
                case GeomAbs_BSplineCurve: {
                    Standard_Real Length = CPnts_AbscissaPoint::Length(adapt);
                    Standard_Real ParLength = adapt.LastParameter() - adapt.FirstParameter();
                    Standard_Real NbrSegments = Round(Length / SegValue.getValue());

                    Standard_Real beg = adapt.FirstParameter();
                    Standard_Real end = adapt.LastParameter();
                    Standard_Real stp = ParLength / NbrSegments;
                    bool reversed = false;
                    if (it2.Orientation() == TopAbs_REVERSED) {
                        std::swap(beg, end);
                        stp = -stp;
                        reversed = true;
                    }

                    if (first) {
                        first = false;
                    }
                    else {
                        beg += stp;
                    }
                    Base::SequencerLauncher seq(

                        "Create waypoints",

                        static_cast<size_t>((end - beg) / stp)

                    );
                    if (reversed) {
                        for (; beg > end; beg += stp) {
                            gp_Pnt P = adapt.Value(beg);
                            Base::Rotation R1;
                            // if orientation is used
                            if (UseRotation.getValue()) {
                                // here get the orientation of the start and end point...
                                // R1 = ;
                            }

                            Waypoint wp("Pt", Base::Placement(Base::Vector3d(P.X(), P.Y(), P.Z()), R1));
                            trac.addWaypoint(wp);
                            seq.next();
                        }
                    }
                    else {
                        for (; beg < end; beg += stp) {
                            gp_Pnt P = adapt.Value(beg);
                            Base::Rotation R1;
                            // if orientation is used
                            if (UseRotation.getValue()) {
                                // here get the orientation of the start and end point...
                                // R1 = ;
                            }
                            Waypoint wp("Pt", Base::Placement(Base::Vector3d(P.X(), P.Y(), P.Z()), R1));
                            trac.addWaypoint(wp);
                            seq.next();
                        }
                    }

                } break;
                case GeomAbs_Circle: {
                    Standard_Real Length = CPnts_AbscissaPoint::Length(adapt);
                    Standard_Real ParLength = adapt.LastParameter() - adapt.FirstParameter();
                    Standard_Real NbrSegments = Round(Length / SegValue.getValue());
                    Standard_Real SegLength = ParLength / NbrSegments;

                    if (it2.Orientation() == TopAbs_REVERSED) {
                        // Beginning and End switch
                        double i = adapt.LastParameter();
                        if (first) {
                            first = false;
                        }
                        else {
                            i -= SegLength;
                        }
                        for (; i > adapt.FirstParameter(); i -= SegLength) {
                            gp_Pnt P = adapt.Value(i);
                            Base::Rotation R1;
                            // if orientation is used
                            if (UseRotation.getValue()) {
                                // here get the orientation of the start and end point...
                                // R1 = ;
                            }
                            Waypoint wp("Pt", Base::Placement(Base::Vector3d(P.X(), P.Y(), P.Z()), R1));
                            trac.addWaypoint(wp);
                        }
                    }
                    else {
                        double i = adapt.FirstParameter();
                        if (first) {
                            first = false;
                        }
                        else {
                            i += SegLength;
                        }
                        for (; i < adapt.LastParameter(); i += SegLength) {
                            gp_Pnt P = adapt.Value(i);
                            Base::Rotation R1;
                            // if orientation is used
                            if (UseRotation.getValue()) {
                                // here get the orientation of the start and end point...
                                // R1 = ;
                            }
                            Waypoint wp("Pt", Base::Placement(Base::Vector3d(P.X(), P.Y(), P.Z()), R1));
                            trac.addWaypoint(wp);
                        }
                    }
                    break;
                }

                default:
                    throw Base::TypeError("Unknown Edge type in Robot::Edge2TracObject::execute()");
            }
        }
    }

    Trajectory.setValue(trac);

    return App::DocumentObject::StdReturn;
}


// short Edge2TracObject::mustExecute(void) const
//{
//     return 0;
// }

void Edge2TracObject::onChanged(const Property* prop)

{
    App::GeoFeature::onChanged(prop);
}