@@ -29,7 +29,7 @@ public function saveDXFFile($filename)
2929 }
3030
3131 // Dessine une forme à une position donnée dans le fichier DXF
32- public function drawShapeAtPositionDXF ($ resizedImage , $ x , $ y , $ toolSizes , $ shape , &$ positions , $ angle = null , $ ignoreThreshold , $ pen )
32+ public function drawShapeAtPositionDXF ($ resizedImage , $ x , $ y , $ toolSizes , $ shape , &$ positions , $ angle = null , $ ignoreThreshold , $ invert , $ pen )
3333 {
3434
3535 // Vérifier que $x et $y sont dans les limites de l'image redimensionnée
@@ -40,12 +40,20 @@ public function drawShapeAtPositionDXF($resizedImage, $x, $y, $toolSizes, $shape
4040 $ grayValue = imagecolorat ($ resizedImage , $ x , $ y ) & 0xFF ; // Extraire la composante grise
4141
4242 // Ignorer les pixels très sombres
43- if ($ grayValue < $ ignoreThreshold ) {
44- return ;
43+ if ($ invert ) {
44+ // Ignorer les pixels très clairs si on inverse la logique
45+ if ($ grayValue > (255 - $ ignoreThreshold )) {
46+ return ;
47+ }
48+ } else {
49+ // Ignorer les pixels très sombres (logique normale)
50+ if ($ grayValue < $ ignoreThreshold ) {
51+ return ;
52+ }
4553 }
46-
54+
4755 // Mapper la nuance de gris sur une taille d'outil dans le tableau généré
48- $ toolSize = $ this ->functionService ->mapGrayToToolSize ($ grayValue , $ toolSizes );
56+ $ toolSize = $ this ->functionService ->mapGrayToToolSize ($ grayValue , $ toolSizes, $ invert );
4957
5058 // Vérifier si l'outil est trop proche des bords pour éviter de dessiner hors de l'image
5159 if ($ x - $ toolSize / 2 < 0 || $ x + $ toolSize / 2 > imagesx ($ resizedImage ) || $ y - $ toolSize / 2 < 0 || $ y + $ toolSize / 2 > imagesy ($ resizedImage )) {
@@ -79,8 +87,16 @@ public function drawShapeAtPositionDXF($resizedImage, $x, $y, $toolSizes, $shape
7987 case 'square ' :
8088 $ this ->writeSquareToDXF ($ x , -$ y , $ toolSize , $ angle , $ pen );
8189 break ;
90+ case 'rectangle ' :
91+ $ this ->writeRectangleToDXF ($ x , -$ y , $ toolSize *2 , $ toolSize ,$ angle , $ pen );
92+ break ;
8293 case 'triangle ' :
83- $ this ->writeTriangleeToDXF ($ x , -$ y , $ toolSize , $ angle , $ pen );
94+ $ this ->writeTriangleToDXF ($ x , -$ y , $ toolSize , $ angle , $ pen );
95+ break ;
96+ case 'hexagon ' :
97+ // Calcul des points pour un hexagone
98+ $ points = $ this ->functionService ->calculateHexagonPoints ($ toolSize );
99+ $ this ->writeHexagonToDXF ($ x , -$ y , $ toolSize , $ angle , $ pen );
84100 break ;
85101 }
86102
@@ -107,7 +123,7 @@ private function writeCircleToDXF($x, $y, $radius, $angle = null, $pen)
107123 fwrite ($ this ->dxfFile , "0 " ); // Fin de l'entité
108124 }
109125
110- private function writeSquareToDXF ($ x , $ y , $ size , $ angle , $ pen ) {
126+ private function writeSquareToDXF ($ x , $ y , $ size , $ angle , $ pen ) {
111127 // Génère un carré au format DXF (les coordonnées des 4 points après rotation)
112128 $ halfSize = $ size / 2 ;
113129
@@ -123,7 +139,24 @@ private function writeSquareToDXF($x, $y, $size, $angle, $pen) {
123139 return $ this ->generateDXFPolygon ($ points , $ pen );
124140 }
125141
126- private function writeTriangleeToDXF ($ x , $ y , $ size , $ angle , $ pen ) {
142+ private function writeRectangleToDXF ($ x , $ y , $ width , $ height , $ angle , $ pen ) {
143+ // Génère un rectangle au format DXF (les coordonnées des 4 points après rotation)
144+ $ halfWidth = $ width / 2 ;
145+ $ halfHeight = $ height / 2 ;
146+
147+ // Calcul des points avec rotation
148+ $ points = $ this ->rotatePoints ([
149+ ['x ' => $ x - $ halfWidth , 'y ' => $ y - $ halfHeight ], // coin supérieur gauche
150+ ['x ' => $ x + $ halfWidth , 'y ' => $ y - $ halfHeight ], // coin supérieur droit
151+ ['x ' => $ x + $ halfWidth , 'y ' => $ y + $ halfHeight ], // coin inférieur droit
152+ ['x ' => $ x - $ halfWidth , 'y ' => $ y + $ halfHeight ], // coin inférieur gauche
153+ ['x ' => $ x - $ halfWidth , 'y ' => $ y - $ halfHeight ], // retour au coin supérieur gauche
154+ ], $ x , $ y , $ angle );
155+
156+ return $ this ->generateDXFPolygon ($ points , $ pen );
157+ }
158+
159+ private function writeTriangleToDXF ($ x , $ y , $ size , $ angle , $ pen ) {
127160 // Génère un triangle au format DXF (les coordonnées des 3 points après rotation)
128161 $ halfSize = $ size / 2 ;
129162 $ points = [
@@ -138,6 +171,33 @@ private function writeTriangleeToDXF($x, $y, $size, $angle, $pen) {
138171 return $ this ->generateDXFPolygon ($ rotatedPoints , $ pen );
139172 }
140173
174+ private function writeHexagonToDXF ($ x , $ y , $ toolSize , $ angle , $ pen ) {
175+ // Calcul des points de l'hexagone
176+ $ points = $ this ->calculateHexagonPoints ($ x , $ y , $ toolSize );
177+
178+ // Appliquer la rotation aux points
179+ $ rotatedPoints = $ this ->rotatePoints ($ points , $ x , $ y , $ angle );
180+
181+ // Générer l'hexagone en DXF avec les points tournés
182+ return $ this ->generateDXFPolygon ($ rotatedPoints , $ pen );
183+ }
184+
185+ public function calculateHexagonPoints ($ x , $ y , $ toolSize ) {
186+ $ radius = $ toolSize / 2 ; // Le rayon du cercle circonscrit
187+ $ angleStep = 60 ; // Chaque angle entre les sommets est de 60 degrés
188+
189+ $ points = [];
190+ for ($ i = 0 ; $ i < 7 ; $ i ++) {
191+ $ angle = deg2rad ($ i * $ angleStep ); // Convertir l'angle en radians
192+ $ points [] = [
193+ 'x ' => $ x + $ radius * cos ($ angle ), // Calcul de la coordonnée x
194+ 'y ' => $ y + $ radius * sin ($ angle ) // Calcul de la coordonnée y
195+ ];
196+ }
197+
198+ return $ points ;
199+ }
200+
141201 private function generateDXFPolygon ($ points , $ pen ) {
142202 // Génère un polygone DXF à partir d'une liste de points
143203 $ dxfData = "0 \nPOLYLINE \n8 \n0 \n66 \n1 \n62 \n{$ pen }\n" ;
0 commit comments