1
1
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.
2
2
3
- import { Matrix4 , ShapeMeshData , XYZ , command } from "chili-core" ;
4
- import { Dimension , PointSnapData } from "../../snap" ;
5
- import { AngleStep , IStep , PointStep } from "../../step" ;
3
+ import { Matrix4 , Plane , PlaneAngle , Precision , ShapeMeshData , XYZ , command } from "chili-core" ;
4
+ import { Dimension , SnapLengthAtPlaneData } from "../../snap" ;
5
+ import { AngleStep , IStep , LengthAtPlaneStep , PointStep } from "../../step" ;
6
6
import { TransformedCommand } from "./transformedCommand" ;
7
7
8
8
@command ( {
@@ -11,75 +11,85 @@ import { TransformedCommand } from "./transformedCommand";
11
11
icon : "icon-rotate" ,
12
12
} )
13
13
export class Rotate extends TransformedCommand {
14
+ private _planeAngle : PlaneAngle | undefined ;
15
+
14
16
protected override transfrom ( point : XYZ ) : Matrix4 {
15
- const normal = this . stepDatas [ 0 ] . view . workplane . normal ;
17
+ const normal = this . stepDatas [ 1 ] . plane ! . normal ;
16
18
const center = this . stepDatas [ 0 ] . point ! ;
17
- const p1 = this . stepDatas [ 1 ] . point ! ;
18
- const v1 = p1 . sub ( center ) ;
19
- const v2 = point . sub ( center ) ;
20
- const angle = v1 . angleOnPlaneTo ( v2 , normal ) ! ;
19
+ const angle = ( this . _planeAngle ?. angle ! * Math . PI ) / 180 ;
21
20
return Matrix4 . createRotationAt ( center , normal , angle ) ;
22
21
}
23
22
24
23
getSteps ( ) : IStep [ ] {
25
24
let firstStep = new PointStep ( "operate.pickFistPoint" ) ;
26
- let secondStep = new PointStep ( "operate.pickNextPoint" , this . getSecondPointData ) ;
25
+ let secondStep = new LengthAtPlaneStep ( "operate.pickNextPoint" , this . getSecondPointData ) ;
27
26
let thirdStep = new AngleStep (
28
27
"operate.pickNextPoint" ,
29
28
( ) => this . stepDatas [ 0 ] . point ! ,
30
29
( ) => this . stepDatas [ 1 ] . point ! ,
31
- this . getThirdPointData ,
30
+ this . getAngleData ,
32
31
) ;
33
32
return [ firstStep , secondStep , thirdStep ] ;
34
33
}
35
34
36
- private readonly getSecondPointData = ( ) : PointSnapData => {
35
+ private readonly getSecondPointData = ( ) : SnapLengthAtPlaneData => {
36
+ const { point, view } = this . stepDatas [ 0 ] ;
37
37
return {
38
- refPoint : ( ) => this . stepDatas [ 0 ] . point ! ,
39
- dimension : Dimension . D1D2D3 ,
40
- plane : ( ) => this . stepDatas [ 0 ] . view . workplane . translateTo ( this . stepDatas [ 0 ] . point ! ) ,
41
- preview : this . linePreview ,
42
- validator : ( p ) => p . distanceTo ( this . stepDatas [ 0 ] . point ! ) > 1e-6 ,
38
+ point : ( ) => point ! ,
39
+ preview : this . circlePreview ,
40
+ plane : ( p : XYZ | undefined ) => this . findPlane ( view , point ! , p ) ,
41
+ validator : ( p : XYZ ) => {
42
+ if ( p . distanceTo ( point ! ) < Precision . Distance ) return false ;
43
+ return p . sub ( point ! ) . isParallelTo ( this . stepDatas [ 0 ] . view . workplane . normal ) === false ;
44
+ } ,
43
45
} ;
44
46
} ;
45
47
46
- private readonly getThirdPointData = ( ) : PointSnapData => {
48
+ private readonly getAngleData = ( ) => {
49
+ const [ center , p1 ] = [ this . stepDatas [ 0 ] . point ! , this . stepDatas [ 1 ] . point ! ] ;
50
+ const plane = this . stepDatas [ 1 ] . plane ?? this . findPlane ( this . stepDatas [ 1 ] . view , center , p1 ) ;
51
+ const points : ShapeMeshData [ ] = [ this . meshPoint ( center ) , this . meshPoint ( p1 ) ] ;
52
+ this . _planeAngle = new PlaneAngle ( new Plane ( center , plane . normal , p1 . sub ( center ) ) ) ;
47
53
return {
48
54
dimension : Dimension . D1D2 ,
49
- preview : this . rotatePreview ,
50
- plane : ( ) => this . stepDatas [ 0 ] . view . workplane . translateTo ( this . stepDatas [ 0 ] . point ! ) ,
51
- validator : ( p ) => {
52
- return (
53
- p . distanceTo ( this . stepDatas [ 0 ] . point ! ) > 1e-3 &&
54
- p . distanceTo ( this . stepDatas [ 1 ] . point ! ) > 1e-3
55
- ) ;
56
- } ,
55
+ preview : ( point : XYZ | undefined ) => this . anglePreview ( point , center , p1 , points ) ,
56
+ plane : ( ) => plane ,
57
57
} ;
58
58
} ;
59
59
60
- private readonly rotatePreview = ( point : XYZ | undefined ) : ShapeMeshData [ ] => {
61
- let p1 = this . meshPoint ( this . stepDatas [ 0 ] . point ! ) ;
62
- let l1 = this . getRayData ( this . stepDatas [ 1 ] . point ! ) ;
63
- let result = [ p1 , l1 , this . meshPoint ( this . stepDatas [ 1 ] . point ! ) ] ;
64
- if ( point ) {
65
- let shape = this . transformPreview ( point ) ;
66
- let l2 = this . getRayData ( point ) ;
67
- result . push ( l2 , shape ) ;
60
+ private anglePreview (
61
+ point : XYZ | undefined ,
62
+ center : XYZ ,
63
+ p1 : XYZ ,
64
+ points : ShapeMeshData [ ] ,
65
+ ) : ShapeMeshData [ ] {
66
+ point = point ?? p1 ;
67
+ this . _planeAngle ! . movePoint ( point ) ;
68
+ const result = [ ...points ] ;
69
+ if ( Math . abs ( this . _planeAngle ! . angle ) > Precision . Angle ) {
70
+ result . push (
71
+ this . meshCreatedShape (
72
+ "arc" ,
73
+ this . _planeAngle ! . plane . normal ,
74
+ center ,
75
+ p1 ,
76
+ this . _planeAngle ! . angle ,
77
+ ) ,
78
+ this . transformPreview ( point ) ,
79
+ ) ;
68
80
}
69
81
return result ;
70
- } ;
71
-
72
- private getRayData ( end : XYZ ) {
73
- let start = this . stepDatas [ 0 ] . point ! ;
74
- let e = start . add ( end . sub ( start ) . normalize ( ) ! . multiply ( 1e6 ) ) ;
75
- return this . getTempLineData ( start , e ) ;
76
82
}
77
83
78
- private readonly linePreview = ( point : XYZ | undefined ) : ShapeMeshData [ ] => {
79
- let p1 = this . meshPoint ( this . stepDatas [ 0 ] . point ! ) ;
80
- if ( ! point ) {
81
- return [ p1 ] ;
82
- }
83
- return [ p1 , this . getTempLineData ( this . stepDatas [ 0 ] . point ! , point ) ] ;
84
+ private readonly circlePreview = ( end : XYZ | undefined ) => {
85
+ const visualCenter = this . meshPoint ( this . stepDatas [ 0 ] . point ! ) ;
86
+ if ( ! end ) return [ visualCenter ] ;
87
+ const { point, view } = this . stepDatas [ 0 ] ;
88
+ const plane = this . findPlane ( view , point ! , end ) ;
89
+ return [
90
+ visualCenter ,
91
+ this . meshLine ( this . stepDatas [ 0 ] . point ! , end ) ,
92
+ this . meshCreatedShape ( "circle" , plane . normal , point ! , plane . projectDistance ( point ! , end ) ) ,
93
+ ] ;
84
94
} ;
85
95
}
0 commit comments