summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/asymptote/CAD.asy
blob: 3be278805ddf216aa046bfb2800452223425d93f (plain)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
struct sCAD
{
  int    nLineGroup = 0; // 0-3

  pen
  // A
  pA,
  pVisibleEdge,            // Sichtbare Kanten
  pVisibleContour,        // Sichtbarer Umriss
  pUsableWindingLength,    // Nutzbare Gewindelänge
  pSystemLine,            // Systemlinie (Stahlbau)
  pDiagramCurve,            // Kurve in Diagrammen
  pSurfaceStructure,        // Oberflächenstrukturen
  // B
  pB,
  pLightEdge,                // Lichtkante
  pMeasureLine,            // Maßlinie
  pMeasureHelpLine,        // Maßhilfslinie
  pMeasureLineBound,        // Maßlinienbegrenzung
  pReferenceLine,            // Hinweislinie
  pHatch,                    // Schraffur
  pWindingGround,            // Gewindegrund
  pDiagonalCross,            // Diagonalkreuz
  pBendLine,                // Biegelinie
  pProjectionLine,        // Projektionslinie
  pGrid,                    // Rasterlinien
  // C
  pC,
  pFreehand,                // Begrenzung abgebrochener oder unterbrochener
  // Schnitte, wenn die Begrenzung
  // keine Mittellinie ist
  // E
  pE,
  pSurfaceTreatmentAllowed,    // Bereich zulässiger Oberflächenbehandlung
  // F
  pF,
  pInvisibleEdge,            // unsichtbare Kante
  pInvisibleContour,        // unsichtbarer Umriss
  // G
  pG,
  pMiddleLine,            // Mittellinie
  pSymmetryLine,            // Symmetrielinie
  pPartialCircle,            // Teilkreis
  pCircularHole,            // Lochkreis
  pDivisionPlane,            // Teilungsebene
  pTransferLine,            // Trajektorien (Übertragungslinien)
  // J
  pJ,
  pCuttingPlane,                // Schnittebene
  pSurfaceTreatmentRequested,    // Bereich geforderter Behandlungen
  // K
  pK,
  pContourBeforeDeformation,    // Umrisse vor Verformung
  pAdjacentPartContour,        // Umrisse angrenzender Teile
  pEndShapeRawMaterial,        // Fertigformen in Rohteilen
  pContourEligibleType,        // Umrisse wahlweiser Ausführungen
  pPartInFrontOfCuttingPlane;    // Teile vor der Schnittebene



  static sCAD Create(int nLineGroup = 1)
  {
    sCAD    cad = new sCAD;
    if ( nLineGroup < 0 )
      nLineGroup = 0;
    if ( nLineGroup > 3 )
      nLineGroup = 3;
    cad.nLineGroup = nLineGroup;

    restricted real[]    dblFullWidth = {0.35mm, 0.5mm, 0.7mm, 1.0mm};
    restricted real[]    dblHalfWidth = {0.18mm, 0.25mm, 0.35mm, 0.5mm};

    pen    pFullWidth = linewidth(dblFullWidth[nLineGroup]);
    pen    pHalfWidth = linewidth(dblHalfWidth[nLineGroup]);

    // Linienarten:
    // A
    cad.pA =
      cad.pVisibleEdge =
      cad.pVisibleContour =
      cad.pUsableWindingLength =
      cad.pSystemLine =
      cad.pDiagramCurve =
      cad.pSurfaceStructure =
      pFullWidth + solid;
    // B
    cad.pB =
      cad.pLightEdge =
      cad.pMeasureLine =
      cad.pMeasureHelpLine =
      cad.pMeasureLineBound =
      cad.pReferenceLine =
      cad.pHatch =
      cad.pWindingGround =
      cad.pDiagonalCross =
      cad.pBendLine =
      cad.pProjectionLine =
      cad.pGrid =
      pHalfWidth + solid;
    // C
    cad.pC =
      cad.pFreehand =
      pHalfWidth + solid;
    // D
    // Missing, as I have no idea how to implement this...
    // E
    cad.pE =
      cad.pSurfaceTreatmentAllowed =
      pFullWidth + linetype(new real[] {10,2.5});
    // F
    cad.pF =
      cad.pInvisibleEdge =
      cad.pInvisibleContour =
      pHalfWidth + linetype(new real[] {20,5});
    // G
    cad.pG =
      cad.pMiddleLine =
      cad.pSymmetryLine =
      cad.pPartialCircle =
      cad.pCircularHole =
      cad.pDivisionPlane =
      cad.pTransferLine =
      pHalfWidth + linetype(new real[] {40,5,5,5});
    // H
    // see J
    // I
    // This letter is not used in DIN 15
    // J
    cad.pJ =
      cad.pCuttingPlane =
      cad.pSurfaceTreatmentRequested =
      pFullWidth + linetype(new real[] {20,2.5,2.5,2.5});
    // K
    cad.pK =
      cad.pContourBeforeDeformation =
      cad.pAdjacentPartContour =
      cad.pEndShapeRawMaterial =
      cad.pContourEligibleType =
      cad.pPartInFrontOfCuttingPlane =
      pHalfWidth + linetype(new real[] {40,5,5,5,5,5});

    return cad;
  } // end of Create



  real    GetMeasurementBoundSize(bool bSmallBound = false)
  {
    if ( bSmallBound )
      return 1.5 * linewidth(pVisibleEdge) / 2;
    else
      return 5 * linewidth(pVisibleEdge);
  }



  path    GetMeasurementBound(bool bSmallBound = false)
  {
    if ( bSmallBound )
      return scale(GetMeasurementBoundSize(bSmallBound = bSmallBound)) *
        unitcircle;
    else
      return (0,0) --
        (-cos(radians(7.5)), -sin(radians(7.5))) *
        GetMeasurementBoundSize(bSmallBound = bSmallBound) --
        (-cos(radians(7.5)), sin(radians(7.5))) *
        GetMeasurementBoundSize(bSmallBound = bSmallBound) --
        cycle;
  }



  void    MeasureLine(picture pic = currentpicture,
                      Label L,
                      pair pFrom,
                      pair pTo,
                      real dblLeft = 0,
                      real dblRight = 0,
                      real dblRelPosition = 0.5,
                      bool bSmallBound = false)
  {
    if ( dblLeft < 0 )
      dblLeft = 0;
    if ( dblRight < 0 )
      dblRight = 0;
    if ( (dblLeft > 0) && (dblRight == 0) )
      dblRight = dblLeft;
    if ( (dblLeft == 0) && (dblRight > 0) )
      dblLeft = dblRight;
    pair    pDiff = pTo - pFrom;
    real    dblLength = length(pDiff);
    pair    pBegin = pFrom - dblLeft * unit(pDiff);
    pair    pEnd = pTo + dblRight * unit(pDiff);
    if ( bSmallBound )
      {
        draw(
             pic = pic,
             g = pBegin--pEnd,
             p = pMeasureLine);
      }
    else
      {
        real dblBoundSize = GetMeasurementBoundSize(bSmallBound = bSmallBound);
        if ( dblLeft == 0 )
          draw(
               pic = pic,
               g = (pFrom + dblBoundSize/2 * unit(pDiff))
               -- (pTo - dblBoundSize/2 * unit(pDiff)),
               p = pMeasureLine);
        else
          draw(
               pic = pic,
               g = pBegin -- (pFrom - dblBoundSize/2 * unit(pDiff))
               ^^ pFrom -- pTo
               ^^ (pTo + dblBoundSize/2 * unit(pDiff)) -- pEnd,
               p = pMeasureLine);
      }
    path    gArrow = GetMeasurementBound(bSmallBound = bSmallBound);
    picture    picL;
    label(picL, L);
    pair    pLabelSize = 1.2 * (max(picL) - min(picL));
    if ( dblLeft == 0 )
      {
        fill(
             pic = pic,
             g = shift(pFrom) * rotate(degrees(-pDiff)) * gArrow,
             p = pVisibleEdge);
        fill(
             pic = pic,
             g = shift(pTo) * rotate(degrees(pDiff)) * gArrow,
             p = pVisibleEdge);
        if ( dblRelPosition < 0 )
          dblRelPosition = 0;
        if ( dblRelPosition > 1 )
          dblRelPosition = 1;
        label(
              pic = pic,
              L = rotate(degrees(pDiff)) * L,
              position =
              pFrom
              + dblRelPosition * pDiff
              + unit(rotate(90)*pDiff) * pLabelSize.y / 2);
      }
    else
      {
        fill(
             pic = pic,
             g = shift(pFrom) * rotate(degrees(pDiff)) * gArrow,
             p = pVisibleEdge);
        fill(
             pic = pic,
             g = shift(pTo) * rotate(degrees(-pDiff)) * gArrow,
             p = pVisibleEdge);
        if ( (dblRelPosition >= 0) && (dblRelPosition <= 1) )
          label(
                pic = pic,
                L = rotate(degrees(pDiff)) * L,
                position =
                pFrom
                + dblRelPosition * pDiff
                + unit(rotate(90)*pDiff) * pLabelSize.y / 2);
        else
          {
            // draw label outside
            if ( dblRelPosition < 0 )
              label(
                    pic = pic,
                    L = rotate(degrees(pDiff)) * L,
                    position =
                    pBegin
                    + pLabelSize.x / 2 * unit(pDiff)
                    + unit(rotate(90)*pDiff) * pLabelSize.y / 2);
            else
              // dblRelPosition > 1
              label(
                    pic = pic,
                    L = rotate(degrees(pDiff)) * L,
                    position =
                    pEnd
                    - pLabelSize.x / 2 * unit(pDiff)
                    + unit(rotate(90)*pDiff) * pLabelSize.y / 2);
          }
      }
  } // end of MeasureLine



  void    MeasureParallel(picture pic = currentpicture,
                          Label L,
                          pair pFrom,
                          pair pTo,
                          real dblDistance,
                          // Variables from MeasureLine
                          real dblLeft = 0,
                          real dblRight = 0,
                          real dblRelPosition = 0.5,
                          bool bSmallBound = false)
  {
    pair    pDiff = pTo - pFrom;
    pair    pPerpendicularDiff = unit(rotate(90) * pDiff);
    real    dblDistancePlus;
    if ( dblDistance >= 0 )
      dblDistancePlus = dblDistance + 1mm;
    else
      dblDistancePlus = dblDistance - 1mm;
    draw(
         pic = pic,
         g = pFrom--(pFrom + dblDistancePlus*pPerpendicularDiff),
         p = pMeasureHelpLine
         );
    draw(
         pic = pic,
         g = pTo--(pTo + dblDistancePlus*pPerpendicularDiff),
         p = pMeasureHelpLine
         );
    MeasureLine(
                pic = pic,
                L = L,
                pFrom = pFrom + dblDistance * pPerpendicularDiff,
                pTo = pTo + dblDistance * pPerpendicularDiff,
                dblLeft = dblLeft,
                dblRight = dblRight,
                dblRelPosition = dblRelPosition,
                bSmallBound = bSmallBound);
  } // end of MeasureParallel



  path MakeFreehand(pair pFrom, pair pTo,
                    real dblRelDivisionLength = 12.5,
                    real dblRelDistortion = 2.5,
                    bool bIncludeTo = true)
  {
    pair    pDiff = pTo - pFrom;
    pair    pPerpendicular = dblRelDistortion * linewidth(pFreehand) *
      unit(rotate(90) * pDiff);

    int nNumOfSubDivisions=ceil(length(pDiff) /
                                (dblRelDivisionLength * linewidth(pFreehand)));

    restricted real[]    dblDistortion = {1, -.5, .75, -.25, .25, -1, .5, -.75,
                                          .25, -.25};
    int                    nDistortion = 0;

    guide    g;
    g = pFrom;
    for ( int i = 1 ; i < nNumOfSubDivisions ; ++i )
      {
        g = g ..
          (pFrom
           + pDiff * i / (real)nNumOfSubDivisions
           + pPerpendicular * dblDistortion[nDistortion]);
        nDistortion += 1;
        if ( nDistortion > 9 )
          nDistortion = 0;
      }

    if ( bIncludeTo )
      g = g .. pTo;

    return g;
  } // end of MakeFreehand



} // end of CAD