空間図形「21年 静岡文芸大 デザイン 7」をsympyとFreeCADでやってみた。

中田島砂丘で...
オリジナル

https://kamelink.com/blog/2022/12/13/6-6-21-shizuokabungeidai-dezain-7/

上と同じです。大学入試数学問題集成＞

https://mathexamtest.web.fc2.com/2021/202111471/2021114710100mj.html#top-0108

# sympyで(オリジナルのやり方)
勉強中

# sympyで(sympy的?安易なやり方)

```python
from sympy import *
var('k l')
A,B=map(Point,([2,0,0],[0, 3,0]))
u,v=map(Point,([2,1,6],[2,-1,3]))
ans=solve([Eq(A.x+k*u.x,B.x+l*v.x),Eq(A.y+k*u.y,B.y+l*v.y),Eq(A.z+k*u.z,B.z+l*v.z)],[k,l])
P=Point(A.x+k*u.x,A.y+k*u.y,A.z+k*u.z).subs({k:ans[k],l:ans[l]})
print("#(1)",P)
C=B+Rational(7,7-3)*(P-B)
print("#(2)",C)
#(1) Point3D(4, 1, 6)
#(2) Point3D(7, -1/2, 21/2)
```
以下の、on line sympyで、上記のソースコードを貼り付けて実行できました。私の環境は,pycharmです。　

https://live.sympy.org/

# sympyでエラーがでます。

```python
from sympy import *
var('k l')
A,B=map(Point,([2,0,0],[0, 3,0]))
u,v=map(Point,([2,1,6],[2,-1,3]))
print(    Eq(A+k*u,B+l*v))
ans=solve(Eq(A+k*u,B+l*v),[k,l])
print("#",ans)
```

pycharmのエラーメッセージです。
```
Traceback (most recent call last):
  File "C:/Users/xxx/PycharmProjects/pythonProject/main.py", line 6, in <module>
(以下省略)
```

sympyliveのエラーメッセージです。
```
TypeError                                 Traceback (most recent call last)
Cell In[2], line 6
      4 u,v=map(Point,([2,1,6],[2,-1,3]))
      5 print(    Eq(A+k*u,B+l*v))
----> 6 ans=solve(Eq(A+k*u,B+l*v),[k,l])
      7 print("#",ans)
(以下省略)
```


# FreeCADのマクロで
マクロからsympyを使っています。

![png1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109698/df36573d-fa42-4a7e-1e94-fdcfe5ebee3d.png)

凧揚げだから、見上げた感じ？

![png3.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109698/cbbb242d-4715-969c-2384-44023e180531.png)


```python
import FreeCAD
import Part
import Draft
import Mesh
#########################################################################################################
from sympy import *
var('k l')
A,B=map(Point,([2,0,0],[0, 3,0]))
u,v=map(Point,([2,1,6],[2,-1,3]))
ans=solve([Eq(A.x+k*u.x,B.x+l*v.x),Eq(A.y+k*u.y,B.y+l*v.y),Eq(A.z+k*u.z,B.z+l*v.z)],[k,l])
P=Point(A.x+k*u.x,A.y+k*u.y,A.z+k*u.z).subs({k:ans[k],l:ans[l]})
print("#(1)",P)
C=B+Rational(7,7-3)*(P-B)
print("#(2)",C)
#(1) Point3D(4, 1, 6)
#(2) Point3D(7, -1/2, 21/2)
#########################################################################################################
# 3D作図
def myXYZ2Txt(A):
    return '(' + str(A.x) + ',' + str(A.y) + ',' + str(A.z) + ')'
def myTxtXYZ(A,myWedgei):
    P5x=float(A.x)
    P5y=float(A.y)
    P5z=float(A.z)
    p5 = FreeCAD.Vector(P5x, P5y, P5z)
    myText = Draft.makeText(myWedgei, p5)
    myText.Label = myWedgei
    FreeCADGui.ActiveDocument.ActiveObject.FontSize = '0.5 mm'
    return
def myTxtXYZ_S(*xy_tx):
    for i in range(1,int(len(xy_tx)/2)+1):
        myTxtXYZ(xy_tx[2*i-2],xy_tx[2*i-1]+myXYZ2Txt(xy_tx[2*i-2]) )
    return
def myLine(A,B):
    Ax,Ay,Az=float(A.x),float(A.y),float(A.z)
    Bx,By,Bz=float(B.x),float(B.y),float(B.z)
    pl = FreeCAD.Placement()
    pl.Rotation.Q = (0.4247081540122249, 0.17592004639554645, 0.33985110062924484, 0.8204732460821097)
    pl.Base = FreeCAD.Vector(-3.9166066876399563, -2.1670824762243774, 1.7495260956243028)
    points = [FreeCAD.Vector(Ax,Ay,Az), FreeCAD.Vector(Bx,By,Bz)]
    line = Draft.make_wire(points, placement=pl, closed=False, face=True, support=None)
    Draft.autogroup(line)
    return
def myLine_S(*args):
    for i in range(1,len(args)):
        myLine(args[i-1],args[i])
    return
myTxtXYZ_S(A,"A",B,"B",C,"C",P,"P")
myLine    (A,P)
myLine    (B,P)
myLine    (P,C)
doc = App.activeDocument()
App.ActiveDocument.addObject("App::Origin", "Origin")
App.ActiveDocument.getObject('Origin').Visibility = True
App.ActiveDocument.recompute()
Gui.activeDocument().activeView().viewAxonometric()
Gui.SendMsgToActiveView("ViewFit")
```