Name Error: App not defined

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
EElliott
Posts: 1
Joined: Thu Sep 16, 2021 5:52 pm

Name Error: App not defined

Post by EElliott »

Probably my bad and not a bug, but I have a strange issue when running a program. My goal at the moment is to start a file based off of the name of another file and import a dxf plus image. I have gotten the code working perfectly when only using one file, however when I try to split it into two it gives me "NameError: name 'App' is not defined" always after clicking okay in the dialogue box, referencing line 73 even if what is on line 73 changes. Even if I change App to FreeCAD or import it differently, or even if I do not have App on line 73 at all it still gives this error. At some point it said "Gui is not defined" instead, at some point it started working great then stopped working again with no change to the code.

Error screenshot is attached and program is below, excuse the sloppy code as I'm very new to this and have been kinda Frankensteining it. I'm guessing it has something to do with my import or macOS but I have no idea. Thanks for any help!

First File

Code: Select all

import sys, os, glob
sys.path.append(os.path.dirname(__file__))
import FreeCAD, FreeCADGui
App = FreeCAD
import ImportGui, importDXF, Part, PartGui, Draft
from PySide import QtGui, QtCore
import open
#import openUI

open.File().exec_()

if open.File().result == open.userCancelled:
	pass

if open.File().result == open.userOK:
	pass
Second File

Code: Select all

class  File(QtGui.QDialog):
	""""""
	def __init__(self):
		super(File, self).__init__()
		self.initUI()
	def initUI(self):
		self.result = userCancelled
		#                            xLoc,yLoc,xDim,yDim
		self.setGeometry(	250, 250, 300, 200)
		self.setWindowTitle("Part Open")
		self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
		#
		self.savePaths = next(os.walk("/Users/FSBMBP005/Dropbox/Programming/projects/"))[1]
		self.date = str(self.savePaths[0])
		self.dateFullPaths = glob.glob("/Users/FSBMBP005/Dropbox/Programming/projects/"+self.date+"/DXF/*.dxf")
		self.datePaths = []
		for i in range(1, len(self.dateFullPaths)):
			singleFile = os.path.basename(self.dateFullPaths[i])
			self.datePaths.append(singleFile)
		#
		self.popupItems1 = (self.savePaths)
		self.popupItems2 = (self.datePaths)
		self.popup1 = QtGui.QComboBox(self)
		self.popup1.addItems(self.popupItems1)
		self.popup1.activated[str].connect(self.onPopup1)
		self.popup1.move(50, 30)
		self.popup2 = QtGui.QComboBox(self)
		self.popup2.addItems(self.popupItems2)
		self.popup2.activated[str].connect(self.onPopup2)
		self.popup2.move(50, 55)
		#
		self.thicknessInput = QtGui.QLineEdit(self)
		self.thicknessInput.setInputMask("99")
		self.thicknessInput.setText("20")
		self.thicknessInput.setFixedWidth(40)
		self.thicknessInput.move(130, 90)
		#
		self.inLabel = QtGui.QLabel("Thickness", self)
		self.inLabel .setFont('Courier')
		self.inLabel .move(51, 96.5)
		#
		cancelButton = QtGui.QPushButton('Cancel', self)
		cancelButton.clicked.connect(self.onCancel)
		cancelButton.setAutoDefault(True)
		cancelButton.move(7, 140)
		#
		okButton = QtGui.QPushButton('OK', self)
		okButton.clicked.connect(self.onOk)
		okButton.move(223, 140)
		#
		self.show()
		#
	def startFile(self):
		self.partThickness = self.thicknessInput.text()
		importDXF.open(u"/Users/FSBMBP005/Dropbox/Programming/Projects/"+self.popup1.currentText()+"/DXF/"+self.popup2.currentText())
		partLines = FreeCAD.ActiveDocument.findObjects("Part::Feature")
		for object in partLines:
			FreeCADGui.Selection.addSelection(object)
		Draft.makeSketch(FreeCADGui.Selection.getSelection(), autoconstraints=True)
		FreeCAD.ActiveDocument.removeObject('Layer003')
		for object in partLines:
			FreeCAD.ActiveDocument.removeObject(object.Label)
		#
		FreeCAD.ActiveDocument.addObject("Part::Extrusion", "Stock")
		extrude = FreeCAD.ActiveDocument.getObject("Stock")
		extrude.Base = FreeCAD.ActiveDocument.getObject("Sketch")
		extrude.Dir = FreeCAD.Vector(0,0,-1)
		extrude.LengthFwd = self.partThickness
		Gui.activeDocument().getObject("Stock").Transparency = 50
		extrude.Solid = False
		#
		imageFile = str(self.popup2.currentText())[:-3]+"png"
		App.activeDocument().addObject('Image::ImagePlane','topImage')
		App.activeDocument().topImage.ImageFile = '/Users/FSBMBP005/Dropbox/Programming/projects/'+self.date+"/DXF/"+imageFile
		App.activeDocument().topImage.Placement = App.Placement(App.Vector(0.000000,0.000000,0.000000),App.Rotation(0.000000,0.000000,0.000000,1.000000))
		App.activeDocument().topImage.XSize=100.00
		App.activeDocument().topImage.YSize=100.00
		App.activeDocument().topImage.Visibility=False
		#
		App.activeDocument().addObject('Image::ImagePlane','bottomImage')
		App.activeDocument().bottomImage.ImageFile = '/Users/FSBMBP005/Dropbox/Programming/projects/'+self.date+"/DXF/"+imageFile
		App.activeDocument().bottomImage.Placement = App.Placement(App.Vector(0.000000,0.000000,-int(self.partThickness)),App.Rotation(0.000000,0.000000,0.000000,1.000000))
		App.activeDocument().bottomImage.XSize=100.00
		App.activeDocument().bottomImage.YSize=100.00
		#
		FreeCAD.ActiveDocument.recompute()
		Gui.SendMsgToActiveView("ViewFit")
		Gui.runCommand('Std_OrthographicCamera',1)
		Gui.Transparency=40
		fileName = str(self.popup2.currentText())[:-4]
		App.ActiveDocument.saveAs(u"/Users/FSBMBP005/Dropbox/Programming/projects/support/#FR33/Scripts/TestParts/"+fileName+".FCStd")
		#import and extrude drawing, import pictures at part thickness, save as file name
	def onPopup1(self, selectedText):
		self.date = selectedText
		self.dateFullPaths = glob.glob("/Users/FSBMBP005/Dropbox/Programming/projects/"+self.date+"/DXF/*.dxf")
		self.datePaths = []
		for i in range(1, len(self.dateFullPaths)):
			singleFile = os.path.basename(self.dateFullPaths[i])
			self.datePaths.append(singleFile)
		self.popup2.clear()
		self.popup2.addItems(self.datePaths)
		print(self.popup2.count())
	def onPopup2(self, selectedText):
		self.partPath = selectedText
	def onCancel(self):
		self.result = userCancelled
		print("Cancelled")
		self.close()
	def onOk(self):
		self.result = userOK
		self.startFile()
		self.close()

userCancelled = "Cancelled"
userOK = "OK"
Error Screenshot.png
Error Screenshot.png (34.44 KiB) Viewed 978 times
OS: macOS High Sierra (10.13)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.24291 (Git)
Build type: Release
Branch: (HEAD detached at 0.19.2)
Hash: 7b5e18a0759de778b74d3a5c17eba9cb815035ac
Python version: 3.8.8
Qt version: 5.12.9
Coin version: 4.0.0
OCC version: 7.4.0
Locale: C/Default (C)
mario52
Veteran
Posts: 4690
Joined: Wed May 16, 2012 2:13 pm

Re: Name Error: App not defined

Post by mario52 »

Hi

Code: Select all

import FreeCAD, FreeCADGui
App = FreeCAD
Gui = FreeCADGui
or

Code: Select all

import FreeCAD as App
or

Code: Select all

App=FreeCAD
mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
Post Reply