Basic steps to assign a texture to a face ?

Need help, or want to share a macro? Post here!
sanderboer
Posts: 21
Joined: Fri Aug 30, 2019 9:47 am

Basic steps to assign a texture to a face ?

Postby sanderboer » Sun Sep 15, 2019 8:59 pm

Hi,

I'm working on a script that downloads an aerial photo of a site and I want to place it in the scene on scale.
I have the photo downloader, the geometry is there, but I am poking in the dark when it comes to mapping an image onto geometry.

Is there a minimal viable example around ? A simple macro that I can look at ?
I know about the arch tectures wb, but I was kinda hoping I would have to tweeze out a workbench for a simple script. 8-)

thanks.

PS, The texturing on draft.rectangle is broken for me on 0.18. It textures the plane, but it is garbled.
mario52
Posts: 2674
Joined: Wed May 16, 2012 2:13 pm

Re: Basic steps to assign a texture to a face ?

Postby mario52 » Mon Sep 16, 2019 8:49 am

Maybe you need a special feature, go into Macros_recipes and Code_snippets, Dialog creation. My macros on Gist.github.
sanderboer
Posts: 21
Joined: Fri Aug 30, 2019 9:47 am

Re: Basic steps to assign a texture to a face ?

Postby sanderboer » Tue Sep 17, 2019 9:02 am

Yes, yes, thank you.

Here is the snippet:
(it relies on owslib, you'll have to adfd that to your python env)

Code: Select all


from owslib.wms import WebMapService
import Part
from pivy import coin

doc=FreeCAD.ActiveDocument
vec = FreeCAD.Base.Vector

def get_luchtfoto(LOC_RD, size):
    url="https://geodata.nationaalgeoregister.nl/luchtfoto/rgb/wms?"
    wms=WebMapService(url)
    img = wms.getmap(layers=['Actueel_ortho25'],
                    srs='EPSG:28992',
                    bbox=(LOC_RD[0]-size, LOC_RD[1]-size, LOC_RD[0]+size, LOC_RD[1]+size),
                    size=(2048,2048),
                    format='image/jpeg',
                    transparent=True
                    )
    filename = 'luchtfoto-'+str(LOC_RD[0])+'_'+str(LOC_RD[1])+'.jpg'   
    out = open(filename, 'wb')
    out.write(img.read())
    out.close()
    p0 = vec(-size*1000,-size*1000,0)
    p1 = vec(size*1000,-size*1000,0)
    p2 = vec(size*1000,size*1000,0)
    p3 = vec(-size*1000,size*1000,0)
    
    rec = [p2,p1,p0,p3,p2]
    shape = Part.makePolygon(rec)
    shp=Part.makeFilledFace(shape.Edges)
    p = doc.addObject("Part::Feature", "rec")
    p.Shape=shp
    rootnode = p.ViewObject.RootNode
    tex =  coin.SoTexture2()
    tex.filename = filename
    rootnode.insertChild(tex,1)
    p.Placement.Rotation= FreeCAD.Base.Rotation(vec(0,0,1),270)


location=(160769, 384904)
get_luchtfoto(location, 200)
sanderboer
Posts: 21
Joined: Fri Aug 30, 2019 9:47 am

Re: Basic steps to assign a texture to a face ?

Postby sanderboer » Tue Sep 17, 2019 9:19 am

In the netherlands you have a bunch of interesting webmapservices and webfeature services, pretty sure other countries have the same.
If you want to take a stab at it, find your country's EPSG and check pyproj.Transformer to transform latlong coords from google maps.

Another question: can I embed a texture in the FCstd ?


cheers,
sander
sanderboer
Posts: 21
Joined: Fri Aug 30, 2019 9:47 am

Re: Basic steps to assign a texture to a face ?

Postby sanderboer » Tue Sep 17, 2019 9:38 am

NB:

the placement is not correct in the above snippet, it needs to be:

Code: Select all

p.Placement.Rotation= FreeCAD.Base.Rotation(vec(1,1,0),180