Bonjour à tous,
Voila, j'ai récupéré une macro en VBScript pour la création de texte 3D. Elle fonctionne correctement, mais il lui manque la gestion de taille de la polices... Ce qui est déjà bien, mais pas top
Berfe, je m'y connais pas suffisamment en macro... Quelqu'un aurait une aide à donner pour completer cette macro?
la dite macro :
' 3D text, (c) ema, lm:19.5.2010, V3
'
' path to the Part with fonts definitions 3DTEXT.CATPart
font_catpart_definition="D:\Mes Documents\CATIA\Macro CATIA V5\texte 3d catia\E3DTEXT.CATPart"
'
dbg=false
'
Language="VBSCRIPT"
Sub CATMain()
' *************************************************************************************
On Error Resume Next
test=1000*55.5+2345.678
if(Err.Number<>0)then
msgbox "Sorry, catia program overflow." & vbCrLf & "Restart catia and try again.", ,msgboxtext
overflow=true
Exit Sub
else
overflow=false
end if
On Error Goto 0
CATIA.DisplayFileAlerts =false
' ******************************* variables *******************************************
Set objNetwork = CreateObject("Wscript.Network" )
msgboxtext="e3Dtext - V2"
dim char_sur(99)
' ******************************* test if part is open ********************************
If CATIA.Documents.Count = 0 Then
MsgBox "There is no CATIA Part file open. Open a Part file and run this script again.", ,msgboxtext
Exit Sub
End If
If InStr(CATIA.ActiveDocument.Name, ".CATPart" ) < 1 Then
MsgBox "Active CATIA Document is not a Part. Open a Part file and run this script again.", ,msgboxtext
Exit Sub
End If
' ******************************* variables *******************************************
set docs=CATIA.Documents
set cad2=CATIA.ActiveDocument
set sel2=cad2.Selection
'Set hb2=cad2.Part.InWorkObject
Set hsf2=cad2.Part.HybridShapeFactory
Set vis2=cad2.Selection.VisProperties
Set spa2=cad2.GetWorkbench("SPAWorkbench" )
Set prt2=cad2.Part
Set bodies1=prt2.Bodies
Set axisSyst2=prt2.AxisSystems
dim surOut(999),lnOut(999)
demo=0
On Error Goto 0
' ******************************* open 3DTEXT.CATPart *********************************
On Error Resume Next
Set cad1=docs.Open(font_catpart_definition)
if(Err.Number<>0)then
msgbox "Can't open the part with fonts definitions [" & font_catpart_definition & "]." & vbCrLf & _
"Check the variable [font_catpart_definition] setting on 3th macro line and start macro again.", ,msgboxtext
exit sub
end if
On Error Goto 0
set sel1=cad1.Selection
Set hsf1=cad1.Part.HybridShapeFactory
Set prt1=cad1.Part
set param1=prt1.Parameters
cad2.activate()
' ******************************* load parameters *************************************
text_spaces=0
text_hight=0
space_width=0
For i = 1 to param1.Count
if(InStrRev(param1.Item(i).Name,"text spaces" ))then text_spaces=param1.Item(i).value
if(InStrRev(param1.Item(i).Name,"text hight" )) then text_hight =param1.Item(i).value
if(InStrRev(param1.Item(i).Name,"space width" ))then space_width=param1.Item(i).value
' if(InStrRev(param1.Item(i).Name,"text ratio" ))then text_ratio =param1.Item(i).value
' if(InStrRev(param1.Item(i).Name,"text ratio" )) then set param_text_ratio =param1.Item(i)
Next
if(text_spaces=0)then
msgbox "Parameter named [text spaces] not found." & vbCrLf & _
"Can't define text spaces value." & vbCrLf & _
"Check parameters in 3DTEXT.CATPart and start macro again.", ,msgboxtext
exit sub
end if
if(text_hight=0)then
msgbox "Parameter named [text hight] not found." & vbCrLf & _
"Can't define text hight value." & vbCrLf & _
"Check parameters in 3DTEXT.CATPart and start macro again.", ,msgboxtext
exit sub
end if
if(space_width=0)then
msgbox "Parameter named [space width] not found." & vbCrLf & _
"Can't define text space width value." & vbCrLf & _
"Check parameters in 3DTEXT.CATPart and start macro again.", ,msgboxtext
exit sub
end if
' if(text_ratio=0)then
' msgbox "Parameter named [text ratio] not found." & vbCrLf & _
' "Can't define text space scale ratio value." & vbCrLf & _
' "Check parameters in 3DTEXT.CATPart and start macro again.", ,msgboxtext
' exit sub
' end if
' ******************************* select curve/edge ***********************************
sel2.Clear
Dim InputObjectType(2)
InputObjectType(0)="BiDimFeatEdge"
InputObjectType(1)="TriDimFeatEdge"
InputObjectType(2)="HybridShapeCurveExplicit"
Status=sel2.SelectElement2(InputObjectType,">>>>>>>>>>>>>> Select a curve <<<<<<<<<<<<<<",false)
' Status=sel2.Search("n:Curve.1,all" )
if (Status = "Cancel" ) then Exit Sub
Set crv = sel2.Item(1).Reference
' ******************************* select crv end point ********************************
if false then ' *** debug ***
n=2
Set hb2 = prt2.HybridBodies.Add()
prt2.Update
else
Set hb2 = prt2.HybridBodies.Add()
prt2.Update
Set pt1= hsf2.AddNewPointOnCurveFromPercent(crv,0.0,true)
hb2.AppendHybridShape pt1
Set pt2= hsf2.AddNewPointOnCurveFromPercent(crv,1.0,true)
hb2.AppendHybridShape pt2
prt2.Update
sel2.Clear
sel2.Add pt1
sel2.Add pt2
vis2.SetSymbolType 4
vis2.SetRealColor 0,255,0,1
sel2.Clear
Dim InputObjectType2(0)
InputObjectType2(0)="ZeroDimFeatVertexOrWireBoundaryMonoDimFeatVertex"
n=0
msg = ">>>>>>>>>>>>>> Select the end of the curve - text starting point. <<<<<<<<<<<<<<"
do
Status=sel2.SelectElement2(InputObjectType2,msg,false)
if (Status = "Cancel" ) then
sel2.clear
sel2.add hb2
sel2.delete
Exit Sub
End If
set pt=cad2.Part.FindObjectByName(sel2.Item(1).reference.parent.name)
If (pt1.name = pt.name) Then n = 1
If (pt2.name = pt.name) Then n = 2
msg = ">>>>>>>>>>>>>> Invalid selection, Select the end of the curve to extract <<<<<<<<<<<<<<"
loop while(n=0)
hsf2.DeleteObjectForDatum pt1
hsf2.DeleteObjectForDatum pt2
end if
' ******************************* select surface **************************************
sel2.Clear
Dim InputObjectType1(0)
InputObjectType1(0)="BiDim"
Status=sel2.SelectElement2(InputObjectType1,">>>>>>>>>>>>>> Select an surface <<<<<<<<<<<<<<",false)
' Status=sel2.Search("n:Surface.37,all" )
if (Status = "Cancel" ) then Exit Sub
Set sur = sel2.Item(1).Reference
' ******************************* key 3D text *****************************************
' str1 = "aBcD"
' str1 = "pokusny text na psani slov"
str1=InputBox("key 3D text ",msgboxtext,"" )
if(str1 = "" )then
msgbox "invalid text or empty !", ,msgboxtext
exit sub
end if
' ******************************* select tooling direction ****************************
Dim InputObjectTypeDir(2)
InputObjectTypeDir(0)="Line"
InputObjectTypeDir(1)="RectilinearMonoDimFeatEdge"
InputObjectTypeDir(2)="RectilinearTriDimFeatEdge"
rc=msgbox(" Select tooling direction .......... YES" & vbCrLf & _
" Use normal direction .............. NO" & vbCrLf,vbYesNoCancel+vbDefaultButton1 ,msgboxtext)
if(rc = vbCancel)then Exit Sub
if(rc = vbYes)then
Status=sel2.SelectElement2(InputObjectTypeDir,"Select an axis or line to set the tooling direction",false)
if(Status = "Cancel" )then Exit Sub
normalDir=false
'On Error Resume Next
set dir = sel2.Item(1).reference
'Set dirX = hsf2.AddNewDirection(dir)
'if(Err.Number<>0)then
' msgbox "invalid element for direction selected" & vbCrLf & "select line or axis", ,msgboxtext
' exit sub
'end if
'On Error Goto 0
else
normalDir=true
end if
' ******************************* text prism height ***********************************
do
parValue="5,5"
parValue = InputBox("Key text prism height H1,H2:",msgboxtext, parValue)
if(parValue = "" )then
msgbox "escape or invalid height !", ,msgboxtext
exit sub
end if
i=InStr(parValue,"," )
if i=0 then
H1=cdbl(parValue)
H2=0
else
H1=cdbl(mid(parValue,1,i-1))
H2=cdbl(mid(parValue,i+1))
end if
if H1+H2=0 then
msgbox "invalid height values !" & vbCrLf & "H1=" & H1 & vbCrLf & "H2=" & H2 & vbCrLf & "sum H1+H2 must be <> 0", ,msgboxtext
else
exit do
end if
loop while true
' ******************************* load characters & numbers ***************************
sel1.Clear
sel1.Add prt1.HybridBodies.Item("fonts" )
sel1.Copy
sel2.clear
sel2.add prt2
sel2.Paste()
Status=sel2.Search("t:surface,sel" )
nfonts=sel2.count
if nfonts=0 then
msgbox "Can't define character shape." & vbCrLf & _
"Nubmer of surfaces-fonts defined in geometrical set [fonts] in 3DTEXT.CATPart is 0." & vbCrLf & _
"Check 3DTEXT.CATPart and start macro again.", ,msgboxtext
exit sub
end if
for i=1 to nfonts
set char_sur(i)=sel2.item(i).value
next
''msgbox char_sur(1).name & " " & char_sur(2).name & " " & nfonts
cad1.close
' ******************************* get text & crv length *******************************
if overflow then
dist=len(str1)*5-1
else
dist=0
for i=1 to len(str1)
char1=ucase(mid(str1,i,1))
k=0
for j=1 to nfonts
if(char1=left(char_sur(j).name,1))then k=j
next
if(k=0)then
char_width=space_width
else
if demo=1 then k=1
char_width=mid(char_sur(k).name,2)
end if
dist=dist+char_width+text_spaces
next
text_length=dist-text_spaces
Set M0 = spa2.GetMeasurable(crv)
crv_length = M0.Length
text_ratio=crv_length/text_length
end if
'msgbox text_length & " " & crv_length & " " & text_ratio
'text_ratio=1
' ******************************* nhr *************************************************
Set specsAndGeomWindow1 = CATIA.ActiveWindow
Set viewer3D1 = specsAndGeomWindow1.ActiveViewer
Set viewpoint3D1 = viewer3D1.Viewpoint3D
viewer3D1.RenderingMode = catRenderWireFrame
' ******************************* go **************************************************
Set axis1 = axisSyst2.Add()
prt2.UpdateObject axis1
AxisDirection=2
num=0
do while true
if AxisDirection=2 then
AxisDirection=0
else
AxisDirection=2
end if
dist=0
if n=1 then
if overflow then
dist=324
else
dist=crv_length
end if
end if
Set pt = hsf2.AddNewPointOnCurveFromDistance(crv,dist,False)
hb2.AppendHybridShape pt
' prt2.Update
if n=1 then
Set lnNormal=hsf2.AddNewLineNormal(sur,pt,0.0,-10.0,False)
else
Set lnNormal=hsf2.AddNewLineNormal(sur,pt,0.0,10.0,False)
end if
hb2.AppendHybridShape lnNormal
' prt2.Update
' if n=1 then
' Set lnTangency = hsf2.AddNewLineTangency(crv,pt,0.0,-10.0,False)
' else
Set lnTangency = hsf2.AddNewLineTangency(crv,pt,0.0,10.0,False)
' end if
hb2.AppendHybridShape lnTangency
prt2.Update
Set axis2 = axisSyst2.Add()
axis2.OriginType = catAxisSystemOriginByPoint
axis2.OriginPoint = pt
if n=1 then
axis2.XAxisType = 2
else
axis2.XAxisType = 0
end if
' axis2.XAxisType = catAxisSystemAxisOppositeDirection
axis2.XAxisDirection = lnTangency
axis2.YAxisType = 0
axis2.ZAxisType = AxisDirection
axis2.ZAxisDirection = lnNormal
prt2.UpdateObject axis2
hsf2.GSMVisibility pt,0
hsf2.GSMVisibility lnNormal,0
hsf2.GSMVisibility lnTangency,0
prt2.Update
sel2.clear
sel2.add axis2
vis2.SetShow 1
'hsf2.GSMVisibility axis2,0
for i=1 to len(str1)
char1=ucase(mid(str1,i,1))
k=0
for j=1 to nfonts
if(char1=left(char_sur(j).name,1))then k=j
next
if(k=0)then
char_width=space_width
else
if demo=1 then k=1
char_width=mid(char_sur(k).name,2)
end if
if overflow then
if n=1 then
dist=dist-2
else
dist=dist+2
end if
else
if n=1 then
dist=dist-text_ratio*char_width/2
else
dist=dist+text_ratio*char_width/2
end if
end if
pt.Ratio.value=dist
prt2.Update
if(k>0)then
Set reference1 = prt2.CreateReferenceFromObject(char_sur(k))
Set reference2 = prt2.CreateReferenceFromObject(axis1)
Set reference3 = prt2.CreateReferenceFromObject(axis2)
Set AxisToAxis1 = hsf2.AddNewAxisToAxis(reference1,reference2,reference3)
hb2.AppendHybridShape AxisToAxis1
prt2.Update
num=num+1
if num>999 then
msgbox "Program limitation/error, number of characters overflow 999.", ,msgboxtext
exit sub
end if
if overflow then
Set surOut(num)=hsf2.AddNewSurfaceDatum(AxisToAxis1)
else
Set hybridShapeScaling1 = hsf2.AddNewHybridScaling(AxisToAxis1,pt,text_ratio)
hb2.AppendHybridShape hybridShapeScaling1
prt2.Update
Set surOut(num)=hsf2.AddNewSurfaceDatum(hybridShapeScaling1)
end if
Set lnOut(num)=hsf2.AddNewLineDatum(lnNormal)
hb2.AppendHybridShape lnOut(num)
hb2.AppendHybridShape surOut(num)
prt2.Update
if not overflow then hsf2.DeleteObjectForDatum hybridShapeScaling1
hsf2.DeleteObjectForDatum AxisToAxis1
end if
if overflow then
if n=1 then
dist=dist-3
else
dist=dist+3
end if
else
if n=1 then
dist=dist-text_ratio*char_width/2-text_ratio*text_spaces
else
dist=dist+text_ratio*char_width/2+text_ratio*text_spaces
end if
end if
next
' ******************************* b o d y ********************************************
Set body1 = bodies1.Add()
Set sf = prt2.ShapeFactory
prt2.Update
for i=1 to num
Set bnd = hsf2.AddNewBoundaryOfSurface(surOut(i))
hb2.AppendHybridShape bnd
Set pad1 = sf.AddNewPadFromRef(bnd,H1)
pad1.SetProfileElement bnd
pad1.SecondLimit.Dimension.Value=H2
if normalDir then
pad1.SetDirection lnOut(i)
else
pad1.SetDirection dir
end if
next
prt2.Update
' ******************************* invert **********************************************
rc=msgbox("Invert 3D text ?" & vbCrLf & " YES ... invert" & vbCrLf & " NO .... exit ",vbYesNo,msgboxtext)
sel2.clear
if(rc = vbNo)then
sel2.Add prt2.HybridBodies.Item("fonts" )
sel2.add axis2
sel2.add lnNormal
sel2.add lnTangency
sel2.add pt
sel2.delete
sel2.add hb2
vis2.SetShow 1
Exit Sub
end if
num=0
sel2.add body1
sel2.add hb2
sel2.add axis2
sel2.delete
Set hb2 = prt2.HybridBodies.Add()
prt2.Update
loop
' *************************************************************************************
' ******************************* E N D *********************************************
' *************************************************************************************
End Sub
Voila, toute aide est la bienvenu et comme ca peut être utile à d'autre, je poste ca ici.
Merci