-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathOutils.bas
More file actions
447 lines (377 loc) · 15.4 KB
/
Copy pathOutils.bas
File metadata and controls
447 lines (377 loc) · 15.4 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
Attribute VB_Name = "Outils"
'******************************************************************************
'*
'* Projet GIRABASE - CERTU - CETE de l'Ouest
'*
'* Module standard : OUTILS.BAS
'*
'* Fonctions Utilitaires diverses
'*
'******************************************************************************
Option Explicit
'*************************************************************************************
' Retourne la plus petite des valeurs du tableau Valeurs
'*************************************************************************************
Public Function Min(ParamArray Valeurs()) As Variant
Dim Valeur As Variant
Min = Valeurs(0)
For Each Valeur In Valeurs
If Valeur < Min Then Min = Valeur
Next
End Function
'*************************************************************************************
' Retourne la plus grande des valeurs du tableau Valeurs
'*************************************************************************************
Public Function Max(ParamArray Valeurs()) As Variant
Dim i As Integer
Max = Valeurs(0)
For i = 1 To UBound(Valeurs)
If Valeurs(i) > Max Then Max = Valeurs(i)
Next
End Function
'******************************************************************************
' Retourne si une feuille est chargée
'******************************************************************************
Public Function EstChargée(ByVal FeuilleCherchée As Form) As Boolean
Dim Feuille As Form
For Each Feuille In Forms
If Feuille Is FeuilleCherchée Then EstChargée = True: Exit For
Next
End Function
'*************************************************************************************
' Existence d'un fichier
' Nom : nom du fichier
' Retourne True si le fichier existe
'*************************************************************************************
Public Function ExistFich(ByVal nom As String, Optional attrib As Variant) As Boolean
On Error GoTo TraitementErreur
If nom = "" Then Exit Function
If IsMissing(attrib) Then
ExistFich = (Dir(nom) <> "")
Else
ExistFich = (Dir(nom, attrib) <> "")
End If
Exit Function
TraitementErreur:
ExistFich = False
Resume Next
End Function
'******************************************************************
' Détecte si le fichier est en lecture seule ou en mise à jour
'******************************************************************
Public Function FichierProtégé(ByVal NomFich As String, Optional ByVal MsgLectureSeule As Boolean = True, Optional ByVal Titre As String, Optional ByVal LectureSeuleAutorisée As Boolean) As Boolean
Dim numFich As Integer
Dim Chaine As String
'Dim f As Scripting.File
'Dim gtFso As New Scripting.FileSystemObject
If NomFich = "" Then Exit Function
If Not ExistFich(NomFich) Then Exit Function
On Error GoTo GestErr
' Détection d'une protection système en écriture
' Set f = gtFso.GetFile(NomFich)
' If (f.Attributes And ReadOnly) = ReadOnly Then Err.Raise 75
numFich = FreeFile
Open NomFich For Append Lock Write As numFich
Close numFich
' Détection d'un verrouillage en écriture par un autre utilisateur
numFich = FreeFile
Open NomFich For Random Lock Write As numFich
Close numFich
If LectureSeuleAutorisée Then FichierProtégé = False
Exit Function
GestErr:
FichierProtégé = True
If Err = 75 Then ' ReadOnly ou Append Lock Write
If MsgLectureSeule Then MsgBox IDm_LectureSeule, vbInformation, Titre
If LectureSeuleAutorisée Then Resume Next
ElseIf Err = 70 Then
MsgBox IDm_FichierUtilisé, vbExclamation, Titre
ElseIf Err = 55 Then ' Append Lock Write
MsgBox IDm_FichierDéjaOuvert & vbCrLf & IDm_EnregistrerSousDabord, vbExclamation, NomFich
Else
'ErrGeneral "Girstand : FichierProtégé"
End If
End Function
'*************************************************************************************
' Extrait le chemin d'un nom de fichier, y compris l'éventuel dernier '\'
'*************************************************************************************
Public Function extraiRep(s As String) As String
Dim pos As Integer
pos = InStrRev(s, "\")
If pos <> 0 Then
extraiRep = Left(s, pos)
End If
Exit Function
pos = InStr(s, "\")
If pos <> 0 Then
extraiRep = Left(s, pos) & extraiRep(Mid(s, pos + 1))
Else
extraiRep = ""
End If
End Function
'*************************************************************************************
' Supprime l'extension dans un nom de fichier (avec ou sans chemin)
'*************************************************************************************
Public Function suppExt(s As String) As String
Dim pos%
' Sous W95 et >, seul le dernier point est un séparateur : d'où InstrRev au lieu de Instr (InstrRev n'existait pas dans VB4)
pos = InStrRev(s, ".")
If pos <> 0 Then
suppExt = Left(s, pos - 1)
Else
suppExt = s
End If
End Function
'*************************************************************************************
' Retourne l'extension d'un nom de fichier (éventuellement en majuscules ou minuscules)
'*************************************************************************************
Public Function Extension(s As String, Optional indic As Variant) As String
Dim pos%
' Sous W95 et >, seul le dernier point est un séparateur : d'où InstrRev au lieu de Instr (InstrRev n'existait pas dans VB4)
pos = InStrRev(s, ".")
If pos = 0 Then
Extension = ""
Else
Extension = Mid(s, pos + 1)
If Not IsMissing(indic) Then Extension = StrConv(Extension, indic)
End If
End Function
'*************************************************************************************
'Extrait le nom principal d'un fichier (sans son chemin ni son extension)
'*************************************************************************************
Public Function nomCourt(s As String) As String
Dim pos%
pos = Len(extraiRep(s))
nomCourt = suppExt(Mid(s, pos + 1))
End Function
'*************************************************************************************
' Redéfinit les dimensions de la feuille indépendamment de la définition et de la résolution de l'écran
'*************************************************************************************
Public Sub SetDeviceIndependentWindow(ByVal ThisForm As Form)
'Static Gauche As Integer, Sommet As Integer, Largeur As Integer, Hauteur As Integer
' Correctif pour écran de haute résolution 2013-04-23
Static Gauche As Single, Sommet As Single, Largeur As Single, Hauteur As Single
With ThisForm
If Largeur = 0 Then
' Définit la largeur de la feuille.
.Width = MDIGirabase.Width * 0.985
' Définit la hauteur de la feuille.
.Height = MDIGirabase.Height * 0.914
.Height = MDIGirabase.Height * 0.866 ' Pour la barre d'outils
Gauche = .Left
Sommet = .Top
Largeur = .Width
Hauteur = .Height
Else ' Une feuille a déjà été chargée
.Move Gauche, Sommet, Largeur, Hauteur
End If
' Positionnement de l'onglet principal à droite de la fenêtre
If ThisForm Is gbProjetActif.Données Then
.tabDonnées.Move .Width - (.tabDonnées.Width + 300), 150
End If
End With
fin:
End Sub
'*************************************************************************************
' Retourne le numéro d'option en cours dans un groupe de boutons
' Retourne -1 si aucun
'*************************************************************************************
Public Function Numopt(ByVal bouton As Object) As Integer
Dim i%
Numopt = -1 ' a priori aucun bouton sélectionné
For i = 1 To bouton.count
If bouton(i - 1) = True Then Numopt = i - 1: Exit Function ' bouton trouvé
Next
End Function
'*****************************************************************************************************
' Contrôle lors de la frappe d'un caractère que celui-ci est bien un nombre décimal
' Le point ou la virgule sont acceptés comme point décimal selon la configuration du poste de travail
'*****************************************************************************************************
Public Function ControleRéel(KeyAscii As Integer) As Integer
If (KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = gbPtDecimal Or KeyAscii = 8 Then
Else
Beep
KeyAscii = 0
End If
ControleRéel = KeyAscii
End Function
'*************************************************************************************
' Controle qu'une donnée est bien numérique, et éventuellement strictement positive
'*************************************************************************************
Public Function MonCtrlNumeric(ByVal v As Object, ByVal Obligatoire As Boolean, ByVal Positif As Boolean) As Boolean
Dim Style As String
Style = vbOKOnly + vbExclamation
If v = "" Then
If Obligatoire Then
MsgBox IDm_Obligatoire, Style
End If
MonCtrlNumeric = Obligatoire
ElseIf Not IsNumeric(v) Then
MsgBox IDm_Numeric, Style
MonCtrlNumeric = True
ElseIf Positif And v <= 0 Then
MsgBox IDm_Positif, Style
MonCtrlNumeric = True
Else
MonCtrlNumeric = False
End If
End Function
'*****************************************************************************************
' Vérifie que la valeur lue dans le fichier est bien un entier et compris entre les bornes
'*****************************************************************************************
Public Sub OkEntier(ByVal Valeur, ByRef variable As Integer, ByVal valMin As Integer, Optional ByVal ValMax)
Dim Ok As Boolean
If VarType(Valeur) = vbInteger Then
If IsMissing(ValMax) Then
Ok = (Valeur >= valMin)
Else
Ok = (Valeur >= valMin) And (Valeur <= ValMax)
End If
If Ok Then
variable = Valeur
Else
Err.Raise 100
End If
Else
Err.Raise 100
End If
End Sub
'*****************************************************************************************
' Vérifie que la valeur lue dans le fichier est bien un entier long et compris entre les bornes
'*****************************************************************************************
Public Sub OkLong(ByVal Valeur, ByRef variable As Long, ByVal valMin As Integer, Optional ByVal ValMax)
Dim Ok As Boolean
If VarType(Valeur) = vbLong Or VarType(Valeur) = vbInteger Then
If IsMissing(ValMax) Then
Ok = (Valeur >= valMin)
Else
Ok = (Valeur >= valMin) And (Valeur <= ValMax)
End If
If Ok Then
variable = Valeur
Else
Err.Raise 100
End If
Else
Err.Raise 100
End If
End Sub
'*****************************************************************************************
' Vérifie que la valeur lue dans le fichier est bien un flottant
'*****************************************************************************************
Public Sub OkFlottant(ByVal Valeur As String, ByRef variable As Single)
Dim pos%
' Dans le fichier, le pt décimal est tjs stocké comme "." - Substitution éventuelle au paramètre régional
' IsNumeric tient compte du paramètre régional
pos = InStr(Valeur, ".")
If pos <> 0 Then Mid(Valeur, pos) = Chr(gbPtDecimal)
If IsNumeric(Valeur) Then
variable = CSng(Valeur)
Else
Err.Raise 100
End If
End Sub
Public Sub ErreurFatale(Optional ByVal NomFonc As String)
MsgBox IDm_ErreurFatale & " " & CStr(Err.Number) & vbCrLf & Err.Description, vbCritical, App.Title
If gbProjetActif Is Nothing Then
End
Else
With gbProjetActif.Données
.flagErreurFatale = True
If .ChargementEnCours Then
Else
Unload gbProjetActif.Données
End If
End With
End If
End Sub
'*************************************************************************************
' Concaténation d'un nombre variable de chaines
'*************************************************************************************
Public Function concatLignes(Chaine As String, ParamArray tabChaines() As Variant) As String
Dim i As Integer
concatLignes = Chaine
If Not IsMissing(tabChaines) Then
For i = 0 To UBound(tabChaines)
concatLignes = concatLignes & vbCrLf & tabChaines(i)
Next
End If
End Function
'''*************************************************************************************
''' substVirgulePoint : remplace le point décimal par une virgule ou réciproquement (issu de GIRATION : périmé cf OKFlottant)
'''*************************************************************************************
''Public Function substVirgulePoint(ByVal s As String) As String
''' If IsNumeric("1.1") Then gbPtDecimal = 46 (.) Else gbPtDecimal = 44 (,)
''' ceci permet aux fontions Cdbl et IsNumeric (en particulier) de fonctionner correctement
''
'' Dim pos%
'' Dim FauxPoint As String * 1
''
'' If gbPtDecimal = Asc(",") Then
'' FauxPoint = "."
'' Else
'' FauxPoint = ","
'' End If
''
'' pos = InStr(s, FauxPoint)
'' If pos <> 0 Then
'' substVirgulePoint = Left(s, pos - 1) & Chr(gbPtDecimal) & substVirgulePoint(Mid(s, pos + 1))
'' Else
'' substVirgulePoint = s
'' End If
''
''End Function
''Public Function substEspaceCRLF(ByVal s As String) As String
''' fonction appelée pour remplacer les retour-chariot par un espace pour les impressions
''
'' Dim pos%
''
'' pos = InStr(s, vbCrLf)
'' If pos <> 0 Then
'' substEspaceCRLF = Left(s, pos - 1) & " " & substEspaceCRLF(Mid(s, pos + 2))
'' Else
'' substEspaceCRLF = s
'' End If
''
''End Function
'************************************************************************************************
' Conversion d'un angle de degré/grade en radian (radian = True) et réciproquement (radian=False)
'************************************************************************************************
Public Function angConv(ByVal x As Single, ByVal radian As Boolean) As Single
' If radian Then angConv = x * pi / 180 Else angConv = x * 180 / pi
' eqvPI vaut 180 ou 200 selon que les unites sont en degrés ou en grades
If radian Then
angConv = x * PI / eqvPI(gbProjetActif.modeangle)
Else
angConv = x * eqvPI(gbProjetActif.modeangle) / PI
End If
End Function
'************************************************************************************************
' Calcul de l'angle directeur du vecteur OM - M(X,Y) - Retourne 0 si M=O
'************************************************************************************************
Public Function AngleDirecteur(ByVal x As Single, ByVal Y As Single) As Single
If Y = 0 Then
If x >= 0 Then AngleDirecteur = 0 Else AngleDirecteur = PI
ElseIf x = 0 Then
If Y > 0 Then AngleDirecteur = PI / 2 Else AngleDirecteur = 3 * PI / 2
Else
AngleDirecteur = Atn(Y / x)
If x < 0 Then AngleDirecteur = AngleDirecteur + PI
If AngleDirecteur < 0 Then AngleDirecteur = AngleDirecteur + 2 * PI
End If
End Function
Public Function Arccos(ByVal x As Double) As Double
' ArcCosinus
Select Case x
Case 1
Arccos = 0
Case -1
Arccos = PI
Case Else
Arccos = Atn(-x / Sqr(-x * x + 1)) + PI / 2
End Select
End Function
Public Function suppCNull(v As String) As String
' Supprime tous les caractères après et y compris le caractère NULL d'une chaine C
suppCNull = Left(v, InStr(v, Chr(0)) - 1)
End Function